Reputation: 1769
I have a form that uses final-form-arrays. The form works and validation works, however, when i make a state change inside the component, it resets all my values.
I was able to replicate the issue with the same example that react-final-form-arrays provides:
https://codesandbox.io/embed/react-final-form-field-arrays-om6p6
I added a button to toggle a state change. Essentially, just try filling values in the form, and then change the state. The form will reset, and i cant figure out why that is the case. If i remove initialValues, this does not happen.
Does anyone know why that may be?
Upvotes: 4
Views: 4973
Reputation: 27
Just memorize your initialValues in an arrow function then pass it to the form component:
const initialValues = useCallback(() => {
return {data: [{}]};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
Pass the function as your Form component's initialValues prop.
Upvotes: 0
Reputation: 274
2 problems.
This is a pretty common beginner mistake, you should read up on how react does reference equality checks to figure out which components to re-render.
Fixed version: https://codesandbox.io/embed/react-final-form-field-arrays-c6hgu
Upvotes: 6