import { Form, Input, Select, Button } from 'antd';
const { Option } = Select;
class PriceInput extends React.Component { static getDerivedStateFromProps(nextProps) { // Should be a controlled component. if ('value' in nextProps) { return { ...(nextProps.value || {}), }; } return null; }
handleNumberChange = e => { const number = parseInt(e.target.value || 0, 10); if (Number.isNaN(number)) { return; } if (!('value' in this.props)) { this.setState({ number }); } this.triggerChange({ number }); };
handleCurrencyChange = currency => { if (!('value' in this.props)) { this.setState({ currency }); } this.triggerChange({ currency }); };
triggerChange = changedValue => { // Should provide an event to pass value to Form. const { onChange } = this.props; if (onChange) { onChange(Object.assign({}, this.state, changedValue)); } };
static getDerivedStateFromProps(nextProps) { // Should be a controlled component. if ('value' in nextProps) { return { ...(nextProps.value || {}), }; } return null; }
1.传出
在 getDerivedStateFromProps 中value关联到组件
1 2 3 4 5 6 7
triggerChange = changedValue => { // Should provide an event to pass value to Form. const { onChange } = this.props; if (onChange) { onChange(Object.assign({}, this.state, changedValue)); } };
近期评论