Is it possible to use react-datepicker with react hooks forms?

后端 未结 1 2096
醉酒成梦
醉酒成梦 2021-02-19 06:17

Is it possible to use react-datepicker with react hooks forms? I tried following sample:

https://codesandbox.io/s/awesome-shape-j0747?fontsize=14&hidenavigation=1&am

相关标签:
1条回答
  • 2021-02-19 06:51

    Please take a look at the Controller doc: https://react-hook-form.com/api/#Controller

    which we are maintaining a codesandbox example for hosting most the external components UI libs' implementations: https://codesandbox.io/s/react-hook-form-controller-079xx

    <Controller
      as={ReactDatePicker}
      control={control}
      valueName="selected" // DateSelect value's name is selected
      onChange={([selected]) => selected}
      name="ReactDatepicker"
      className="input"
      placeholderText="Select date"
    />
    

    EDIT

    with the latest version of react-hook-form this is the Controller implementation using render:

                <Controller
                    name={name}
                    control={control}
                    render={({ onChange, value }) => (
                        <DatePicker
                            selected={value}
                            onChange={onChange}
                        />
                    )}
                />
    
    0 讨论(0)
提交回复
热议问题