简体   繁体   English

Reactjs antd defaultValue return undefined

[英]Reactjs antd defaultValue return undefined

im trying to make an edit function, the problem come when im trying to get the value from the form, say a user is editing his name, but he does not edit his address, this will make address is undefined even though i already set a defaultValue inside it, this works before in my non antd form, here is my code我试图进行编辑 function,当我试图从表单中获取值时出现问题,说用户正在编辑他的名字,但他没有编辑他的地址,这将使地址未定义,即使我已经设置了里面的defaultValue ,这在我的非antd形式之前有效,这是我的代码

form:形式:

<Form onFinish={this.edit}>
            <Typography>Email</Typography>
            <Form.Item
            name="email"
            >
                <Input defaultValue={user.Email} value={user.Email} className="email form-control col-sm-6"/>
            </Form.Item>

            <Typography>Full Name</Typography>
            <Form.Item
            name="name"
            >
                <Input defaultValue={user.FullName} value={user.FullName} className="email form-control col-sm-6"/>
            </Form.Item>
            <Typography>Admin</Typography>
            <Form.Item
            name="admin"
            >
            {
                (user.IsAdministrator)
                ?   <div> 
                  <Select value="yes">Admin
                  <Select.Option value="no">Not Admin</Select.Option>
                  </Select>
                  </div>
                : <div>
                  <Select value="no">Not admin
                  <Select.Option value="yes">Admin</Select.Option>
                  </Select>
                  </div>  
            }
            </Form.Item>
            <Typography>Active Status</Typography>
            <Form.Item
            name="aktif"
            >
            {
                (user.IsActive)
                ? <Toggle className="switch-lg" checked/>
                : <Toggle className="switch-lg"/>
            }
            </Form.Item>
            <Form.Item>
            <Button type="primary" htmlType="submit" className="login col-md-10">Submit</Button>

and here is how i get the value:这是我获得价值的方式:

edit(values){
        const name = values.name
        const aktif = values.aktif
        const index = this.state.id
        console.log(values)
    }

thanks before, anyhelp will be appreciated之前谢谢,任何帮助将不胜感激

As mentioned in the docs ,如文档中所述

You cannot set value for each form control via value or defaultValue prop, you should set default value with initialValues of Form .您不能通过valuedefaultValue为每个表单控件设置值,您应该使用ForminitialValues设置默认值。

Example below:下面的例子:

<Form
      onFinish={onFinish}
      initialValues={{
        residence: ['zhejiang', 'hangzhou', 'xihu'],
        prefix: '86',
      }}
    >

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM