Commit 1c1d0a9
fix: Ensure initial values are reapplied after destroyOnUnregister in StrictMode (#1069)
* fix: Ensure initial values are reapplied after destroyOnUnregister in StrictMode
Fixes #1031
Problem:
When using destroyOnUnregister with React 18 StrictMode, fields would
lose their initial values. This happened because:
1. StrictMode mounts components twice
2. First mount: field registers with initial value
3. StrictMode unmounts: field unregisters, destroyOnUnregister removes it
4. StrictMode remounts: field tries to register again, but final-form
thinks initial values haven't changed (they're 'equal'), so doesn't
reapply them
Solution:
Before registering a field, check if it doesn't exist in form state
(indicating it was destroyed). If destroyed and we have an initial value,
explicitly set the value via form.change() before registering. This ensures
initial values are always reapplied when a field re-registers after being
destroyed.
The fix:
- Only affects fields that were destroyed (don't exist in form state)
- Checks both field initialValue and form initialValues
- Happens before registration, so it's applied correctly
- No impact on normal usage (only triggers when field was destroyed)
Works with:
- React 18 StrictMode + destroyOnUnregister
- Normal production builds (no change in behavior)
- Initial values from both field config and form initialValues
* fix: Handle form-level initialValues in StrictMode re-mount
Richard's feedback: The guard check was too narrow. It only checked for
field-level initialValue but ignored form-level initialValues.
Changed the check from:
if (!existingFieldState && initialValue !== undefined)
To:
if (!existingFieldState)
And then check both form-level and field-level initial values inside.
This ensures that fields with only form-level initial values (no field-level
initialValue) are also correctly restored after StrictMode unmount/remount.
* chore: Remove accidentally committed backup file
* fix: Handle undefined initialValues in getIn calls
TypeScript was correctly flagging that formState.initialValues could be
undefined. Added checks before calling getIn to avoid type errors.
* fix: Rename unused defaultIsEqual to _defaultIsEqual
ESLint requires unused variables to match /^_/u pattern.
* fix: Remove unused defaultIsEqual variable entirely
Per Erik's feedback - no need to keep it even with underscore prefix.
---------
Co-authored-by: erikras-gilfoyle-agent <gilfoyle@openclaw.local>
Co-authored-by: erikras-dinesh-agent <dinesh@openclaw.dev>
Co-authored-by: Erik Rasmussen <erik@mini.local>1 parent d3adf45 commit 1c1d0a9
1 file changed
+17
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
30 | | - | |
31 | | - | |
32 | 30 | | |
33 | 31 | | |
34 | 32 | | |
| |||
117 | 115 | | |
118 | 116 | | |
119 | 117 | | |
120 | | - | |
| 118 | + | |
121 | 119 | | |
122 | 120 | | |
123 | 121 | | |
| |||
155 | 153 | | |
156 | 154 | | |
157 | 155 | | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
158 | 172 | | |
159 | 173 | | |
160 | 174 | | |
| |||
0 commit comments