Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DestroyOnUnregister incompatible with StrictMode in React18 #1031

Open
ohlsen3000 opened this issue May 17, 2023 · 2 comments
Open

DestroyOnUnregister incompatible with StrictMode in React18 #1031

ohlsen3000 opened this issue May 17, 2023 · 2 comments

Comments

@ohlsen3000
Copy link

ohlsen3000 commented May 17, 2023

Type of issue

Bug

What is the current behavior?

When opening a form with the attribute 'destroyOnUnregister' in StrictMode locally, the initial Values are not displayed.

What is the expected behavior?

When opening a form with the attribute 'destroyOnUnregister' in StrictMode locally, the initial Values are displayed.

Sandbox Link

See https://codesandbox.io/s/inspiring-cori-smyfhm?file=/src/App.js

What's your environment?

react-final-form: 6.5.9
final-form: 4.20.9
react: 18.2

Other information

As far as I can see, the values are rendered in the first cycle. Then StrictMode unmounts the fields, causing them to be removed from the form model (caused by 'destroyOnUnregister'). On the second mount, the initial values are considered to be identical, hence the fields will not be filled again but remain empty.

When running in production (i.e. w/o StrictMode) the form is filled. The form will also work correctly if:

  • You disable StrictMode locally
  • You remove the destroyOnUnregister attribute
  • You overwrite 'initialValuesEqual' with () => false
@gertdreyer
Copy link
Collaborator

Strict Mode is a Breaking Change in React 18...
It emulates unmounting and remounting the fields which would then obviously clear all the values from the formState.

There are workarounds out there e.g. https://blog.ag-grid.com/avoiding-react-18-double-mount/

Willing to accept a PR that fixes this behaviour.

@Axnyff
Copy link

Axnyff commented Nov 15, 2023

Hi!
I recently did a project on which we relied a lot on destroyOnUnregister and I'd be hard to implement the same feature with a FormSpy so I looked into your suggestion.

It's a bit complicated to try and handle correctly the behavior when you have dependencies or changing effect function ( the code in the link broke quite a lot of tests in final-form

I've managed to have a version that works here ( and for which the tests passes on react-final-form )
https://github.com/Axnyff/useEffectOnceInStrictMode/blob/main/useEffectOnceInStrictMode.ts
with some tests here
https://github.com/Axnyff/useEffectOnceInStrictMode/blob/main/useEffectOnceInStrictMode.test.js

I'm not sure I'm not missing some weird edge cases that would break the regular behavior of useEffect.
It seems to fix the behavior of destroyOnUnregister if I only fix that useEffect: https://github.com/final-form/react-final-form/blob/main/src/useField.js#L113

It looks like it would be hard to bring the tests in react-final-form as they are only valid for react 18.
Do you think there's a way I can do a PR to fix that behavior ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants