Cannot update during an existing state transition (such as within `render`). Render methods should b

背景

在这里插入图片描述

在使用antd 4.x的时候 函数组件更新props 中传来的 input 初始值的时候出现,
Warning截图

原代码
const Item=Form.Item;
const UpdateForm=(props)=>{

    const [form] = Form.useForm();
    form.setFieldsValue({'title':props.title});
    const changeHandle=(e)=>{
        const title=e.target.value;
        props.change(title);
    }
    return (    
        <Form form={form} initialValues=''>
            <Item name='title' rules={
                [{
                    required:true,
                    message:'内容不能为空'
                }]
            }>
                <Input  onChange={e=>changeHandle(e)}/>
            </Item>
        </Form>
    )
}
修改后
const Item=Form.Item;
const UpdateForm=(props)=>{

    const [form] = Form.useForm();
    const changeHandle=(e)=>{
        const title=e.target.value;
        props.change(title);
    }
    useEffect(() => {
        form.setFieldsValue({'title':props.title});
    }, [props.title])
    return (    
        <Form form={form} initialValues=''>
            <Item name='title' rules={
                [{
                    required:true,
                    message:'内容不能为空'
                }]
            }>
                <Input  onChange={e=>changeHandle(e)}/>
            </Item>
        </Form>
    )
}
结论

不能在函数组件中直接更新state,(应该时不能在初始化组件的时候更新状态) ,需要放在操作函数中,自己写的change函数或者类似声明周期钩子useEffect中。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值