You cannot set field before registering it. when using forms in react

use the form form in, ant design in react to report You cannot set field before registering it. when dealing with the error message returned by the background Mistakes, although the meaning is easy to understand, but solve the difficulties? Hope to meet the same problem of the gods to guide the confusion. The problem caused by the use of this.props.form.setFields is involved.

Jun.09,2021

it is possible that you set up a Field value, solution that does not exist originally, or add input to hidden to see the official issues


to see setFieldsValue ({.params}), the assigned variable (params), whether there are any variables registered in getFieldDecorator (param).


this.props.form.setFields

can only be used in lifecycle functions after render, because fields is registered in the render function


processing in render

render() {
    const {getFieldProps} = this.props.form;
    return (
        <List>
            <InputItem {...getFieldProps('username')}/>
            <InputItem {...getFieldProps('password')}/>
        </List>
    );
}
If

is not in render, you usually set it manually in componentDidMount .

this.props.form.setFieldsValue({
username:'xxx',
password:'xxx'
})

I also reported this error today, and finally found that destroyOnClose= {true} was set in the Modal box, causing the error!


I also encountered this problem today. My problem is that the note used in
this.props.form.setFieldsValue ({note: msg})
in the lifecycle method componentDidMount is not registered with getFieldDecorator, so if an error is reported, just change the getFieldDecorator to note.

              <FormItem {...formItemLayout} label="">
                {getFieldDecorator('note', {
                  rules: [
                    {
                      required: getFieldValue('reasonable') === 'reasonable' ? true : false,
                    },
                  ],
                })(
                  <Input/>
                )}
              </FormItem>
Menu