defaultValues that contains a date is not rendering in input #7162
-
I am quite new to react and javascript so please excuse me if this is a bit of a rudimentary question or whether in fact this is a bug. The simplified code is below or can be seen at https://codesandbox.io/s/react-form-date-issue-327fh?file=/src/App.tsx:48-63
This gives me the following form which is prepopulated except for the dd/mm/yyyy enter image description here The problem I have is I have a simple react hook form that uses a type which is used by useForm. In this case its called Person I can change the dob input to be of type text rather than date and the correct value is displayed but then there is of course no validation of the date or a date picker Rather than passing in the type to defaultValues of useform I have also tried passing in a defaultValue to each of the inputs. This renders the date correctly (if i output the date to a string of correct format) but I would rather not have to set the defaultValue on each input as the type I am actually using is quite large and I thought it was quite nice just to bind the one object
I know that a input of date requires the defaultvalue to be a text value. However, if I set the default value using defaultvalues in useForm I have no control over how the Input is initialised Any help would be greatly appreciated |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 5 replies
-
Method 1: Working CodeSandBox here. Method 2: I would prefer you the 1st method. Hoping that it would help you. |
Beta Was this translation helpful? Give feedback.
-
Thanks for your suggestion @fahadsohail482. However, to do that I would still have to change the type of the dob from Date to any. What if I dont have control over the Person object? |
Beta Was this translation helpful? Give feedback.
-
Thanks @fahadsohail482. I can work with that. Excuse me if this is a basic question but is there a reason that react-hook-form could not not take care of this in the background? |
Beta Was this translation helpful? Give feedback.
-
Thanks @fahadsohail482. I suspected that was the answer |
Beta Was this translation helpful? Give feedback.
Method 1:
@kiwiinlondon you may want to use the
reset()
function fromuseform()
on mount and set your values as you want. This way you have full control over the values you set to the form.Here is the snippet on how you can do it.
Working CodeSandBox here.
Method 2:
you can use
setValue()
function the similar way but its for the single value usesetValue("name","value")
.I would prefer you the 1st method. Hoping that it would help you.