天天看點

Antd版本V3-->V4遷移問題:初始化調整

本文介紹個常見問題

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

 隻有在初始化以及重置表單時生效。

.