2019-07-03 20:14:39 +08:00
---
2020-10-08 13:15:13 +08:00
title: Migrate Form to v4
2019-07-03 20:14:39 +08:00
skip: true
---
### Remove Form.create
2020-04-03 22:45:39 +08:00
Form of v4 does not need to create context by calling `Form.create()` . Form now has it's own data scope and you don't need `getFieldDecorator` anymore. Just use Form.Item directly:
2019-07-03 20:14:39 +08:00
```jsx
// antd v3
const Demo = ({ form: { getFieldDecorator } }) => (
< Form >
< Form.Item >
{getFieldDecorator('username', {
rules: [{ required: true }],
})(< Input / > )}
< / Form.Item >
< / Form >
);
const WrappedDemo = Form.create()(Demo);
```
To:
```jsx
// antd v4
const Demo = () => (
< Form >
< Form.Item name = "username" rules = {[{ required: true } ] } >
< Input / >
< / Form.Item >
< / Form >
);
```
2020-04-03 22:45:39 +08:00
Since `Form.create()` is removed, methods like `onFieldsChange` have moved to `Form` and form state is controlled by a `fields` prop. ref [example ](/components/form/#components-form-demo-global-state )。
2019-07-03 20:14:39 +08:00
### Form control
If you want to control form, you can use `Form.useForm()` to create Form instance for operation:
```jsx
// antd v3
const Demo = ({ form: { setFieldsValue } }) => {
React.useEffect(() => {
setFieldsValue({
username: 'Bamboo',
});
}, []);
return (
< Form >
< Form.Item >
{getFieldDecorator('username', {
rules: [{ required: true }],
})(< Input / > )}
< / Form.Item >
< / Form >
);
};
const WrappedDemo = Form.create()(Demo);
```
To:
```jsx
// antd v4
const Demo = () => {
const [form] = Form.useForm();
React.useEffect(() => {
form.setFieldsValue({
username: 'Bamboo',
});
}, []);
return (
< Form form = {form} >
< Form.Item name = "username" rules = {[{ required: true } ] } >
< Input / >
< / Form.Item >
< / Form >
);
};
```
For class component, you can use `ref` to access instance:
```jsx
// antd v4
class Demo extends React.Component {
formRef = React.createRef();
componentDidMount() {
2020-02-29 17:09:34 +08:00
this.formRef.current.setFieldsValue({
2019-07-03 20:14:39 +08:00
username: 'Bamboo',
});
}
render() {
return (
2020-03-04 10:26:55 +08:00
< Form ref = {this.formRef} >
2019-07-03 20:14:39 +08:00
< Form.Item name = "username" rules = {[{ required: true } ] } >
< Input / >
< / Form.Item >
< / Form >
);
}
}
```
2020-03-09 17:55:22 +08:00
If you don't want to use the Item style, you can use `noStyle` prop to remove it:
2019-07-03 20:14:39 +08:00
```jsx
// antd v3
2021-01-08 10:14:01 +08:00
const Demo = ({ form: { getFieldDecorator } }) => {
2019-07-03 20:14:39 +08:00
return < Form > {getFieldDecorator('username')(< Input / > )}< / Form > ;
};
const WrappedDemo = Form.create()(Demo);
```
To:
```jsx
// antd v4
const Demo = () => {
return (
< Form >
2019-07-19 14:07:39 +08:00
< Form.Item name = "username" noStyle >
2019-07-03 20:14:39 +08:00
< Input / >
< / Form.Item >
< / Form >
);
};
```
### Linkage with field
2020-04-03 22:45:39 +08:00
New Form uses incremental update which only updates related field. So if there is some linkage between fields or updates with the whole form, you can use [`dependencies` ](/components/form/#dependencies ) or [`shouldUpdate` ](/components/form/#shouldUpdate ) to handle that.
2019-07-03 20:14:39 +08:00
### replace onSubmit with onFinish
2020-04-03 22:45:39 +08:00
You need to listen to `onSubmit` and call `validateFields` to handle validation in old Form. New Form provides `onFinish` which will only trigger when validation has passed:
2019-07-03 20:14:39 +08:00
```jsx
// antd v3
const Demo = ({ form: { getFieldDecorator, validateFields } }) => {
const onSubmit = e => {
e.preventDefault();
validateFields((err, values) => {
if (!err) {
console.log('Received values of form: ', values);
}
});
};
return (
< Form onSubmit = {onSubmit} >
< Form.Item >
{getFieldDecorator('username', {
rules: [{ required: true }],
})(< Input / > )}
< / Form.Item >
< / Form >
);
};
const WrappedDemo = Form.create()(Demo);
```
To:
```jsx
// antd v4
const Demo = () => {
const onFinish = values => {
console.log('Received values of form: ', values);
};
return (
< Form onFinish = {onFinish} >
< Form.Item name = "username" rules = {[{ required: true } ] } >
< Input / >
< / Form.Item >
< / Form >
);
};
```
2019-07-04 15:00:11 +08:00
## Replace validateFieldsAndScroll with scrollToField
2020-04-03 22:45:39 +08:00
New version recommend use `onFinish` for submit after validation. Thus `validateFieldsAndScroll` is changed to more flexible method `scrollToField` :
2019-07-04 15:00:11 +08:00
```jsx
// antd v3
onSubmit = () => {
form.validateFieldsAndScroll((error, values) => {
// Your logic
});
};
```
To:
```jsx
// antd v4
onFinishFailed = ({ errorFields }) => {
form.scrollToField(errorFields[0].name);
};
```
2019-07-03 20:14:39 +08:00
### Initialization
Besides, we move `initialValue` into Form to avoid field with same name both using `initialValue` to cause conflict:
```jsx
// 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);
```
To:
```jsx
// antd v4
const Demo = () => (
< Form initialValues = {{ username: ' Bamboo ' } } >
< Form.Item name = "username" rules = {[{ required: true } ] } >
< Input / >
< / Form.Item >
< / Form >
);
```
In v3, modifying the `initialValue` of un-operated field will update the field value synchronously, which is a bug. However, since it has been used as a feature for a long time, we have not fixed it. In v4, the bug has been fixed. `initialValues` only takes effect when initializing and resetting the form.
### Nested field paths using arrays
In the past versions we used `.` to represent nested paths (such as `user.name` to represent `{ user: { name: '' } }` ). However, in some backend systems, `.` is also included in the variable name. This causes the user to need additional code to convert, so in the new version, nested paths are represented by arrays to avoid unexpected behavior (eg `['user', 'name']` ).
Therefore, paths returned by methods such as `getFieldsError` are always in an array form for the user to handle:
```jsx
form.getFieldsError();
/*
[
{ name: ['user', 'name'], errors: [] },
{ name: ['user', 'age'], errors: ['Some error message'] },
]
*/
```
2020-03-03 10:51:54 +08:00
Nested field definition has changed from:
```jsx
// antd v3
2020-03-09 17:55:22 +08:00
< Form.Item label = "Firstname" > {getFieldDecorator('user.0.firstname', {})(< Input / > )}< / Form.Item >
2020-03-03 10:51:54 +08:00
```
2020-03-09 17:55:22 +08:00
To
2020-03-03 10:51:54 +08:00
```jsx
// antd v4
2020-03-18 11:03:33 +08:00
< Form.Item name = {['user', 0 , ' firstname ' ] } label = "Firstname" >
2020-03-03 10:51:54 +08:00
< Input / >
< / Form.Item >
```
Similarly using `setFieldsValue` has changed from:
```jsx
// antd v3
this.formRef.current.setFieldsValue({
2020-03-09 17:55:22 +08:00
'user.0.firstname': 'John',
2020-03-03 10:51:54 +08:00
});
```
To
```jsx
// antd v4
this.formRef.current.setFieldsValue({
2020-03-09 17:55:22 +08:00
user: [
{
firstname: 'John',
},
],
2020-03-03 10:51:54 +08:00
});
```
2019-07-03 20:14:39 +08:00
### Remove callback in validateFields
`validateFields` will return a Promise, so you can handle the error with `async/await` or `then/catch` . It is no longer necessary to determine if `errors` is empty:
```jsx
// antd v3
validateFields((err, value) => {
if (!err) {
// Do something with value
}
});
```
To
```jsx
// antd v4
validateFields().then(values => {
// Do something with value
});
```