How to reset antd datepicker after submiting a value?

房东的猫 提交于 2019-12-11 18:43:16

问题


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


回答1:


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




回答2:


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}
/>


来源:https://stackoverflow.com/questions/54920833/how-to-reset-antd-datepicker-after-submiting-a-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!