antd Form报错:Warning: You cannot set a form field before rendering a field associated with the value.

news/2024/7/15 18:38:56 标签: react.js, antd

一、报错信息:

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 警告总结


http://www.niftyadmin.cn/n/1557469.html

相关文章

[iOS Swift] 自定义转场动画 UIViewControllerAnimatedTransitioning 报错

swift3.0中, 自定义转场动画要遵循UIViewControllerAnimatedTransitioning协议的时候 报错 解决办法 如下 在写好两个箭头处的代理方法后 会报错 必须补完方框中的两个方法 才不会报错 可能在Swift3.0中 UIViewControllerAnimatedTransitioning 协议必须要求我们设置自定…

antd Tree 组件 带搜索

前言&#xff1a; antd Tree组件带搜索&#xff0c;官网示例感觉很麻烦&#xff0c;不容易让人懂&#xff0c;我就自己实现了一个。 antd 3.xTree带搜索(官网示例) antd 4.x Tree带搜索&#xff08;官网示例&#xff09; 实现代码&#xff1a; 一、这个是搜索到后&#xf…

VMware虚拟机安装 CentOS 6.9

一、应用场景想学习CentOS&#xff0c;自己动手安装个虚拟机。二、安装前的准备2.1 安装软件1.VMware Workstation 12&#xff08;或更高版本&#xff09;&#xff1b;2.XShell 61&#xff09;官网下载XShell 6&#xff1a;https://www.netsarang.com/download/down_form.html?…

Java之品优购课程讲义_day05(2)

1.2.1 前端代码&#xff08;1&#xff09;修改 itemCatService.js //根据上级 ID 查询下级列表 this.findByParentIdfunction(parentId){return $http.get(../itemCat/findByParentId.do?parentIdparentId);} &#xff08;2&#xff09;修改 itemCatController.js //根据上级…

antd 报错 value.locale is not a function

报错信息&#xff1a; value.locale is not a function Uncaught Error: The value/defaultValue of DatePicker or MonthPicker must be a moment object after 从截图不难看出是&#xff0c;DatePicker或者 RangePicker 时间选择器使用时的错误。 1.给DatePicker或者 Range…

mssql sqlserver 获取指定日期为本月第几个工作日

摘要:下文讲述工作中&#xff0c;需要获取指定日期在当月的工作日** 下文讲述常规的指定工作日所在月的天数分析&#xff0c;实现思路:1 生成一个国家法定假日表(A)&#xff0c;非星期六&#xff0c;星期天2 生成一个国家法定补办表(B)&#xff0c;涉及星期六星期天调班3 生成指…

[iOS AutoLayout动画 坑] AutoLayout动画平移坑总结 = Swift/OC

参考帖子:自动布局 Autolayout 报错&#xff1a;Unable to simultaneously satisfy constraints. 参考帖子: Auto Layout 进阶 参考帖子:谈StoryBoard上AutoLayout的约束动画 学习尝试使用Swift做约束动画 实现这样的效果 >> 往下刷 << ⬇️⬇️⬇️ 结果 我…