通过 antd input 组件分析受控与非受控组件
前言
1. 分析过程与结果
const value = typeof props.value === 'undefined' ? props.defaultValue : props.value;
this.state = {
value,
};if (!('value' in this.props)) { this.setState({ value: value }); }renderInput(prefixCls: string) { const { value } = this.state; return this.renderLabeledIcon( <input value={fixControlledValue(value)} />, ); }static getDerivedStateFromProps(nextProps: InputProps) { if ('value' in nextProps) { return { value: nextProps.value, }; } return null; }
2. 查看历史版本
3. 疑问
Last updated