question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

initialValues along with useField yields undefined initially

See original GitHub issue

I 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:closed
  • Created 4 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
joepuzzocommented, Apr 30, 2019

I think i have solved this!

1reaction
joepuzzocommented, Apr 2, 2019

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found