antd4.0中Form使用initialValue

参考文献:
https://blog.csdn.net/weixin_42661440/article/details/107581753
https://blog.csdn.net/jx950915/article/details/107164894/
在antd3中,initialValue是个好东西,设置默认值很方便。在antd4中,设置initialValue,这样我每次编辑回显的时候把数据传过来就行,后来发现不得行!

然后我用resetFields()这个方法,每次缺点或者取消之后重置一下数据,然而我试了还是不得行,变成每次点击显示的是上一次的数据,所以后面还是老老实实看了一下form的其他方法。

原因

在这里插入图片描述
在这里插入图片描述

form API

在这里插入图片描述
我们可以用setFieldsValue动态设置表单的值,在我们打开弹窗的时候,把值设置成我们想要的值,就完美解决。✿✿ヽ(°▽°)ノ✿

代码

 const onAdd = (item: any, type: string) => {
    
    
    if (type === 'edit') {
    
    
      form.setFieldsValue({
    
    
        name: item?.title
      })
    }
    setCreateVisible(true);//打开弹窗
  };
 <Form.Item
   label="名称"
    name="name"
    rules={
    
    [
      {
    
    
        required: true,
        message: '请输入名称',
      },
    ]}
  >
    <Input placeholder='请输入名称' />
  </Form.Item>

猜你喜欢

转载自blog.csdn.net/qq_40657321/article/details/118017826