一、报错信息:
Warning: You cannot set a form field before rendering a field associated with the value.
警告:在呈现与值关联的字段之前,不能设置表单字段。
原因:这个是使用Form表单时会出现,原因时使用表单setFieldsValue时,有的字段设置了,但Form里不存在这个field。需要保证赋值的数据中的各项要在form的field中。
解决方案:
示例:
Form里有两个字段name和password.
一开始setFieldsValue多了一个value。这个字段在Form里没有,所以会出现这个问题。
赋值时的字段一定要和Form里的一致。
import React, { Component } from 'react';
import { Form,Input } from 'antd';
class Index extends Component {
formRef = React.createRef();
constructor(props) {
super(props);
this.state = {
}
}
componentDidMount() {
this.formRef.current.setFieldsValue({ //这样就会出现这个警告 因为 value这个字段,Form里不存在
name: 'Hi, man!',
password:"123",
value:"8888"
});
// this.formRef.current.setFieldsValue({ //这样就行了,所以需要去掉没有的字段
// name: 'Hi, man!',
// password:"123"
// });
}
numhandleChange = (value, e) => {
}
render() {
console.log(this)
return (
<div>
<Form ref={this.formRef}>
<Form.Item
name="username"
label="用户名"
rules={[
{
required: true,
},
]}
>
<Input />
</Form.Item>
<Form.Item
name="password"
label="密码"
rules={[
{
required: true,
},
]}
>
<Input />
</Form.Item>
</Form>
</div>
)
}
}
export default Index
antd Form API
更多警告可以看看下面的文章:
antd 警告总结