initialValues along with useField yields undefined initially
See original GitHub issueI must say that was rather a painful moment before I’ve debugged what’s happening there. Apparently, the initialValues
are ignored until the field is registered. I am not sure if it’s an attempt for some optimization or a limitation of architecture, but I think it’s wrong.
<Form initialValues={{ listOfSomething: [] }}><FormSection /></Form>
const FormSection = () => {
const { fieldState } = useField('listOfSomething')
// fieldState is undefined on initial render
}
One can argue that I should be checking for value there to increase encapsulation and not rely on that initialValues will be set. That’s a fair assumption, but it doesn’t feel right to be forced to do it every time.
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
<Field /> and undefined initial value fires error #321 - GitHub
if value in is undefined React throws error Warning: A component is changing an uncontrolled input of type text to be controlled.
Read more >Building forms using Formik with the React Hooks API and ...
The initialValues prop represents the initial values of our fields. ... With useField() hook, we can create more advanced inputs that look ...
Read more >useFormik() | Formik
A custom React Hook that returns Formik states and helpers. It is used internally to create the <Formik> component, but is exported for...
Read more >useField - VeeValidate
useField is a custom composition API function that allows you to create data models that's automatically validated that you can then use to...
Read more >reactjs - Formik <FieldArray> initialValue undefined if using ...
The state is updated and therefore, I assume the initialValues will update the values object. Scope inside the render method does not allow...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I think i have solved this!
So here is the issue with this. I designed informed with a key point in mind. And that is all fields should be independent of one another and independent of a Form if they want to be. BUT if they were within a form, then they would register themselves witht that form. Because of this however, until the field registers itself with the form then the useField wont have the registered data, i have been thinking a lot about this and may be able to do some playing to get registration to happen on initial render tho