How to reset antd datepicker after submiting a val

2019-09-20 19:06发布

here i am providing my sample example working on codesandbox. How to reset a datepicker value after submitting a form?

    state = {
        setFieldValue: ''
    }

    onChange = (setFieldValue) => {
        this.setState({ setFieldValue: null })
      }


    render() {
        const { values, handleSubmit } = this.props
        return (
            <div align="center">
                <Form onSubmit={handleSubmit}>

                             <Field
                                name="dateofbirth"
                                label="dateOfBirth"
                                component={DateInput}
                                formitemlayout={formItemLayout}
                                value={this.state.setFieldValue}
                                onChange={this.onChange}


                            />


                            <Button type="primary" 
          htmlType="submit">Submit</Button>
}

my working codesandbox link is enter link description here

2条回答
Juvenile、少年°
2楼-- · 2019-09-20 19:21

Instead of adding empty strings as it raises a propType error its best to use null

<DatePicker
  onChange={(date, dateString) =>
    setFieldValue("dateofbirth", dateString)
  }
  value={dateofbirth !== "" ? moment(dateofbirth) : null}
/>
查看更多
闹够了就滚
3楼-- · 2019-09-20 19:26

Your Datepicker is not a controlled component. I converted it to a controlled component and date field was reset post form submission.

<DatePicker
  onChange={(date, dateString) =>
    setFieldValue("dateofbirth", dateString)
  }
  value={dateofbirth !== "" ? moment(dateofbirth) : ""}
/>

Codesandbox link

查看更多
登录 后发表回答