本文介紹個常見問題
Antd的V3---V4版本遷移問題,將 initialValue
從字段中移到 Form 中。以避免同名字段設置 initialValue
的沖突問題:
// antd v3 const Demo = ({ form: { getFieldDecorator } }) => ( <Form> <Form.Item> {getFieldDecorator('username', { rules: [{ required: true }], initialValue: 'Bamboo', })(<Input />)} </Form.Item> </Form> ); const WrappedDemo = Form.create()(Demo);
改成:
// antd v4 const Demo = () => ( <Form initialValues={{ username: 'Bamboo' }}> <Form.Item name="username" rules={[{ required: true }]}> <Input /> </Form.Item> </Form> );
在 v3 版本中,修改未操作的字段 initialValue
會同步更新字段值,這是一個 BUG。但是由於被長期作為一個 feature 使用,因而我們一直沒有修復。在 v4 中,該 BUG 已被修復。initialValue
只有在初始化以及重置表單時生效。
.