Search code examples
javascriptreactjsantd

Warning: ant Checkbox: value is not a valid prop, do you mean checked?


While working with Form in ant design v4, I have difficulty with using CheckBox inside Form.Item.

const [form] = useForm();

useEffect(() => {
    form.setFieldsValue({
        title: '',
        billable: false,
    })
}, []);

const onFieldsChange = (changedFields, allFields) => {
    console.log('onFieldsChange.changedFields => ', changedFields);
    console.log('onFieldsChange.allFields=> ', allFields);
}

return (
    <Form form={form} onFieldsChange={onFieldsChange}>
        <Form.Item label="title" name="title">
            <Input />
        </Form.Item>
        <Form.Item label="Billable" name="billable">
            <CheckBox />
        </Form.Item>
    </Form>
);

Above code gives me the following error:

Warning: [ant:Checkbox] value is not a valid prop, do you mean checked?

How can I use CheckBox inside the Form.Item in ant design v4?


Solution

  • Use valuePropName for Checkbox form item:

    <Form.Item label="Billable" name="billable" valuePropName="checked">
      <Checkbox />
    </Form.Item>
    

    Default value of valuePropName is value which is correct for input fields but not for a checkbox or switch.