fix(useField): Fix no re-render on initialValue change
Currently useField doesn't retriggers properly if the initialValue is changed (Added test scenario). Added explicit check for initial value change to prevent deepEqual overheads.
This pull request is automatically built and testable in CodeSandbox.
To see build info of the built libraries, click here or the icon next to each commit SHA.
Latest deployment of this branch, based on commit fe2fb36682b5981b441038a86301b3883c5ce8c7:
| Sandbox | Source |
|---|---|
| final-form/react-final-form | Configuration |
| final-form/react-final-form | Configuration |
| final-form/react-final-form | Configuration |
| final-form/react-final-form | Configuration |
| final-form/react-final-form | Configuration |
IMO this is fine, you should not listen for initialValues after form initialization. Why you need this?
@bslipek In data driven forms (Ref: https://github.com/data-driven-forms/react-forms ) for conditionally hiding the fields, the fields are wrapped with a Field component so that they get re-rendered when the value in any of the parent attribute change(Ref: https://github.com/data-driven-forms/react-forms/blob/84c4cb8861d689cb87ba68100fe7d3acfd667655/packages/react-form-renderer/src/form-renderer/render-form.js#L47). So, due to the above issue the conditional visibility doesn't works if we reverse the order of the fields(Ref: https://stackblitz.com/edit/1dkk8s-rd1ejn?file=schema.js).
I got your point. But still think that this is not final-form problem. But I am just a observer here :)
Everything is in the name of the props "initialValue"
Thanks for this PR! There's an interesting design question here.
The issue: useField doesn't re-render when initialValue prop changes after mount.
Community perspective (from comments):
@bslipek and @EndMove suggest initialValue is intentionally for initial values only, not dynamic updates. If you need dynamic values, you should update form state directly rather than changing initialValue.
Code concerns:
- Uses
!=instead of!==for comparison - Logic seems backwards: setState only on initial render if initial changed?
- The test doesn't actually verify the behavior works
Design question for @erikras:
Should initialValue be reactive (re-initialize when it changes), or is it intentionally only for initial mount?
- If reactive: This needs better implementation
- If not reactive: Close this PR and document that
initialValueis for initialization only
My instinct: The name "initialValue" suggests it's for initialization, not dynamic updates. For dynamic defaults, use form-level initialValues or form.change().
What's your intent here? 🤔
@coderabbitai review
Review triggered. Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.✅ Actions performed
This PR fixes initial value propagation in the 🎯 2 (Simple) | ⏱️ ~10 minutes 🐰 A hop through the fields, where values now bloom,
Initial ones fresh, dispelling the gloom,
The listener arrives, no longer too late,
For proper state updates—what a fortunate fate! ✏️ Tip: You can configure your own custom pre-merge checks in the settings.📝 Walkthrough
Walkthrough
useField hook. The change modifies how state updates are handled in a useEffect callback, ensuring initial values from Form or Field components are correctly reflected in listeners that mount after the initial Field render.Changes
Cohort / File(s)
Summary
State Management Fix
src/useField.jsRenamed state parameter to newState and restructured conditional logic to properly handle initial value updates on first render versus subsequent renders, ensuring delayed listeners receive correct initial values.
Test Coverage
src/useField.test.jsAdded test case validating that useField correctly reflects initial values when a listener is rendered after Field initialization.
Estimated code review effort
Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3 | ❌ 1
❌ Failed checks (1 warning)
Check name
Status
Explanation
Resolution
Merge Conflict Detection
⚠️ Warning
❌ Merge conflicts detected (25 files):
⚔️ .babelrc.js (content)
⚔️ .github/workflows/ci.yml (content)
⚔️ .gitignore (content)
⚔️ README.md (content)
⚔️ examples/async-redux-submission/asyncSubmissionMiddleware.js (content)
⚔️ examples/async-redux-submission/store.js (content)
⚔️ examples/field-arrays/package.json (content)
⚔️ examples/styling-with-smooth-ui/index.js (content)
⚔️ package-scripts.js (content)
⚔️ package.json (content)
⚔️ src/Field.test.js (content)
⚔️ src/FormSpy.test.js (content)
⚔️ src/ReactFinalForm.test.js (content)
⚔️ src/renderComponent.test.js (content)
⚔️ src/useConstantCallback.test.js (content)
⚔️ src/useField.test.js (content)
⚔️ src/useForm.test.js (content)
⚔️ src/useFormState.test.js (content)
⚔️ typescript/Field.test.tsx (content)
⚔️ typescript/FormSpy.test.tsx (content)
⚔️ typescript/ReactFinalForm.test.tsx (content)
⚔️ typescript/index.d.ts (content)
⚔️ typescript/tsconfig.json (content)
⚔️ typescript/useField.test.tsx (content)
⚔️ typescript/useFormState.test.tsx (content)
These conflicts must be resolved before merging into main.Resolve conflicts locally and push changes to this branch.
✅ Passed checks (3 passed)
Check name
Status
Explanation
Description Check
✅ Passed
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
✅ Passed
The title clearly and specifically describes the main fix: addressing the issue where useField does not re-render when the initialValue prop changes after mount.
Docstring Coverage
✅ Passed
No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
🧪 Generate unit tests (beta)
⚔️ Resolve merge conflicts (beta)
fix_no_render
Comment @coderabbitai help to get the list of available commands and usage tips.
Thank you for identifying a real issue with initialValue change detection in useField, @jspannu919! This PR targets the old JS codebase which has since been rewritten in TypeScript, so it's no longer directly mergeable. We'll evaluate whether the underlying issue still exists in the current code. Closing with appreciation. 🙏
Update: Verified that this bug is fixed in the current TypeScript codebase. The useEffect in useField.ts that registers the field now includes initialValue in its dependency array, so changes to initialValue trigger re-registration and a re-render.