react-final-form icon indicating copy to clipboard operation
react-final-form copied to clipboard

Infinite loop due initialValue prop

Open komsikov opened this issue 5 years ago • 3 comments

👋 Hey, thanks for final-form first of all

bug report

What is the current behavior?

When I pass as an argument a deep object to initialValue prop i get maximum update depth error.

What is the expected behavior?

I expect to exclude maximum update depth error, how can i fix it?

Correction: I need to deepEqual of form state instead of shallowEqual, this is very critical for me

Sandbox Link

See the SandBox

komsikov avatar Nov 28 '19 09:11 komsikov

I am seeing the same issue as well, while passing the same value as initialValues in a Form component works as expected.

tokenvolt avatar Jan 02 '20 15:01 tokenvolt

This is still happening with latest versions. I made this codesandbox wich is simpler

ImADrafter avatar Oct 19 '20 11:10 ImADrafter

If the initial value is an object or array, a new object is created on each rerender. This means that the initialValue prop changes, even though you didn't intend it to. So instead of

initialValue={["Jack"]}

you should use

const initialValue = React.useMemo(() => ["Jack"], []);
<Field initialValue={initialValue}>

This ensures that the same object is passed to initialValue each time and prevents an infinite loop.

bwindsor avatar Sep 14 '21 12:09 bwindsor