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

fix(useField): Fix no re-render on initialValue change

Open jspannu919 opened this issue 3 years ago • 5 comments

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.

jspannu919 avatar May 20 '23 18:05 jspannu919

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

codesandbox-ci[bot] avatar May 20 '23 18:05 codesandbox-ci[bot]

IMO this is fine, you should not listen for initialValues after form initialization. Why you need this?

bslipek avatar May 25 '23 07:05 bslipek

@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).

jspannu919 avatar May 26 '23 02:05 jspannu919

I got your point. But still think that this is not final-form problem. But I am just a observer here :)

bslipek avatar May 26 '23 08:05 bslipek

Everything is in the name of the props "initialValue"

EndMove avatar Apr 14 '25 07:04 EndMove

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 initialValue is 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? 🤔

erikras-richard-agent avatar Feb 05 '26 01:02 erikras-richard-agent

@coderabbitai review

erikras-richard-agent avatar Feb 13 '26 12:02 erikras-richard-agent

✅ Actions performed

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.

coderabbitai[bot] avatar Feb 13 '26 12:02 coderabbitai[bot]

📝 Walkthrough

Walkthrough

This PR fixes initial value propagation in the 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.js
Renamed 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.js
Added test case validating that useField correctly reflects initial values when a listener is rendered after Field initialization.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • final-form/react-final-form#1060: Addresses useField's initial-value handling on first render, modifying the same hook implementation to ensure form/field initial values are correctly reflected.

Suggested reviewers

  • erikras

Poem

🐰 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!

🚥 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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • [ ] 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • [ ] Create PR with unit tests
  • [ ] Post copyable unit tests in a comment
⚔️ Resolve merge conflicts (beta)
  • [ ] Auto-commit resolved conflicts to branch fix_no_render
  • [ ] Post resolved changes as copyable diffs in a comment

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar Feb 13 '26 12:02 coderabbitai[bot]

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. 🙏

erikras-richard-agent avatar Feb 13 '26 12:02 erikras-richard-agent

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.

erikras-richard-agent avatar Feb 13 '26 13:02 erikras-richard-agent