简体   繁体   English

反应:使用 antd 清除后,将 select 的值设置为 null

[英]React: set select's value to null after clear it using antd

I am using ant design on my application.我在我的应用程序中使用 ant 设计。 There is form.有形式。

        useEffect(() => {
         ...props.client
        let client = {
            employeeId:props.client?.employee.id
        };

        form.setFieldsValue(client);
       }, [props.client])

On update form 'employeeId' field is not sending to backend, but i want to set its value to null.在更新表单“employeeId”字段未发送到后端,但我想将其值设置为 null。 Does someone now best way of that?有人现在最好的方法吗?

    <Form.Item name="employeeId">
     <Select allowClear>
       {props.employees?.map((employee) => (
         <Option key={employee.id} value={employee.id}>{employee.name}</Option>
           ))}
            </Select>
            </Form.Item>

I think your useEffect maybe a little buggy .我认为您的useEffect可能有点buggy I don't know what the ...props.client is doing there.我不知道...props.client在那里做什么。

It might help to destructure the client prop as it is used so many times.它可能有助于解构client道具,因为它被使用了很多次。 Putting a snippet below to see if it works for you.在下面放一个片段,看看它是否适合你。

const Component = (props) => {
  const { client, ...otherProps } = props;

  useEffect(() => {
    // if id is present, use it, else set null. 
    // Notice the use of ?? instead of || here. 
    // That is so that id with value 0 is not ignored as it is falsy in nature
    form.setFieldsValue({ employeeId: client?.employee?.id ?? null });
    }, [client]) 

}

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

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