final-form / final-form/react-final-form

keepDirtyOnReinitialize gets active from the next render

Open
#702 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
7.4k
Forks
497
PR merge metrics
No merged PRs in 30d

Description

Having a:
```

```
`keepDirtyOnReinitialize` behavior gets active, not for the current render, but from the next render.
I have traced the issue down to `ReactFinalForm.js`:
```
useWhenValueChanges(
initialValues,
() => {
form.setConfig('initialValues', initialValues)
},
initialValuesEqual || shallowEqual
)
useWhenValueChanges(keepDirtyOnReinitialize, () => {
form.setConfig('keepDirtyOnReinitialize', keepDirtyOnReinitialize)
})
```
These hooks get executed in order, so `initialValues` initialize the form, even before `keepDirtyOnReinitialize` trigger its value.
Simple exchange the two fragments place, fixes the problem.
```
useWhenValueChanges(keepDirtyOnReinitialize, () => {
form.setConfig('keepDirtyOnReinitialize', keepDirtyOnReinitialize)
})
useWhenValueChanges(
initialValues,
() => {
form.setConfig('initialValues', initialValues)
},
initialValuesEqual || shallowEqual
)
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.