Skip to content

Fix #1050: useField returns Form initialValues on first render#1060

Merged
erikras merged 1 commit intomainfrom
fix/issue-1050-usefield-initial-value-clean
Feb 4, 2026
Merged

Fix #1050: useField returns Form initialValues on first render#1060
erikras merged 1 commit intomainfrom
fix/issue-1050-usefield-initial-value-clean

Conversation

@erikras-richard-agent
Copy link
Contributor

@erikras-richard-agent erikras-richard-agent commented Feb 4, 2026

Issue: In v7.0.0, useField returns undefined on first render even when Form initialValues are set. This breaks apps trying to upgrade from v6.

Root cause: useField's useState initializer falls back to creating initial state when getFieldState() returns undefined, but only checks field-level initialValue, not the Form's initialValues.

Solution: In the fallback case, check form.getState().initialValues[name] before using field-level initialValue. Form-level initialValues take precedence.

Tests:

  • useField returns Form initialValues on first render
  • useField uses field initialValue when Form initialValues missing that field
  • All existing tests pass (14/14)

This is a clean solution that avoids the side effects of synchronous registration during render (which React forbids).

Closes #1050

Summary by CodeRabbit

  • Tests

    • Expanded coverage for field initial-value handling, including nested and array field paths and render-time verification.
  • Bug Fixes

    • Fields now prioritize form-level initial values with field-level fallback.
    • Multi-selects initialize to an empty array when no initial value is provided.

@coderabbitai
Copy link

coderabbitai bot commented Feb 4, 2026

Warning

Rate limit exceeded

@erikras-richard-agent has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 0 minutes and 38 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📝 Walkthrough

Walkthrough

Initialization logic in useField was changed to derive the field's initial value from the Form's initialValues (via getIn) before falling back to the field-level initialValue. Tests were added to validate form-level, field-level, nested, and array-path initial value resolution on first render.

Changes

Cohort / File(s) Summary
Tests
src/useField.test.js
Expanded tests to cover initial value resolution: preference for Form initialValues, fallback to field-level initialValue, nested path (user.name) handling, array path (items[0].name) handling, and render-time value assertions via a render spy.
Hook Implementation
src/useField.ts
Use getIn(formState.initialValues, name) to source the initial field value before using field initialValue; consolidate initialStateValue logic and ensure multi-select initializes to [] when appropriate.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐇 I rummaged through values, near and far,
Found forms and fields both know who they are,
Nested names and arrays align,
No more undefined on first draw—it's fine,
Hooray, a proper initial-value carrot! 🥕

🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: fixing useField to return Form initialValues on first render, directly addressing issue #1050.
Linked Issues check ✅ Passed All coding requirements from issue #1050 are met: useField now reads Form-level initialValues before field-level fallback, returns correct values on first render, uses form.getState().initialValues to avoid side effects during render.
Out of Scope Changes check ✅ Passed All changes are directly scoped to resolving issue #1050: test coverage for Form initialValues handling and useField production logic modifications are both within scope.

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

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/issue-1050-usefield-initial-value-clean

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

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@src/useField.ts`:
- Around line 116-125: The lookup for form initial values uses direct property
access (form.getState().initialValues?.[name]) which breaks for path-style names
like "user.name" or "items[0].id"; change it to use Final Form's path-aware
getter (getIn) on the form state initialValues when computing formInitialValue
so formInitialValue = getIn(form.getState().initialValues, name) (falling back
to existing initialValue and preserving the existing select/multiple -> []
handling for initialStateValue). Ensure you import/use the same getIn utility
used elsewhere in the repo and keep references to name, initialValue,
initialStateValue unchanged.

@erikras-richard-agent erikras-richard-agent force-pushed the fix/issue-1050-usefield-initial-value-clean branch from 65f0e9b to dccbeb6 Compare February 4, 2026 18:12
@erikras-richard-agent
Copy link
Contributor Author

@coderabbitai review

@coderabbitai
Copy link

coderabbitai bot commented Feb 4, 2026

✅ 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]
coderabbitai bot previously approved these changes Feb 4, 2026
Issue: In v7.0.0, useField returns undefined on first render even when Form
initialValues are set. This breaks apps trying to upgrade from v6.

Root cause: useField's useState initializer falls back to creating initial
state when getFieldState() returns undefined, but only checks field-level
initialValue, not the Form's initialValues.

Solution: In the fallback case, use getIn() to read form.getState().initialValues
for the field name before using field-level initialValue. This correctly handles
nested field paths like 'user.name' or 'items[0].id'. Form-level initialValues
take precedence.

Tests:
- useField returns Form initialValues on first render
- useField uses field initialValue when Form initialValues missing that field
- useField handles nested field paths (user.name)
- useField handles array field paths (items[0].name)
- All existing tests pass (16/16)

This is a clean solution that avoids the side effects of synchronous
registration during render (which React forbids).
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

Successfully merging this pull request may close these issues.

2 participants