Fix #1093: useField read current value on the render#1094
Conversation
📝 WalkthroughWalkthrough
ChangesuseField initial state derivation
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
Fixes #1094 |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/useField.ts`:
- Around line 129-144: The select-multiple normalization in useField is creating
separate empty arrays for value and initialStateValue when one side is
undefined, which makes the default isEqual check in pristine treat equivalent
empty selections as dirty. Update the empty-array handling inside the multiple
branch so that when one side is already [], the undefined side reuses that same
array instance instead of allocating a new one. Keep the fix localized to the
value/initialStateValue normalization logic in useField.
- Around line 124-128: The value selection in useField is treating undefined
from formState.values as “missing,” which causes a cleared field to fall back to
the initial value. Update the initial value resolution in useField so that, when
the form has an initial value for the same path, the current form state is
always preferred even if it is undefined; only use the field-level initialValue
when the field value is truly absent before registration. Keep the fix localized
to the initialStateValue/value logic and preserve the existing behavior for
untouched fields.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro
Run ID: 6752aecd-953d-46c2-99b2-a5c6f532f24d
📒 Files selected for processing (2)
src/useField.test.jssrc/useField.ts
There was a problem hiding this comment.
♻️ Duplicate comments (1)
src/useField.ts (1)
127-132: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick winMirror this precedence in the pre-registration effect.
Lines 127-132 preserve a cleared current value on first render, but Lines 198-203 can still overwrite it with
formInitialValuebefore registration completes whenexistingFieldStateis missing. Use the same current-value precedence there so a cleared field is not reset during mount.🐛 Proposed fix
if (!existingFieldState) { const formState = form.getState(); const formInitialValue = formState.initialValues ? getIn(formState.initialValues, name) : undefined; - const valueToSet = formInitialValue !== undefined ? formInitialValue : initialValue; + const formValue = formState.values + ? getIn(formState.values, name) + : undefined; + const valueToSet = + formInitialValue !== undefined + ? formValue + : formValue !== undefined + ? formValue + : initialValue; if (valueToSet !== undefined) { form.change(name as keyof FormValues, valueToSet); } }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/useField.ts` around lines 127 - 132, The pre-registration effect in useField is not using the same value precedence as the initial value resolution, so a cleared current value can be overwritten by formInitialValue before registration completes. Update the effect that runs before registration to follow the same precedence as the value logic around formInitialValue, formValue, and initialStateValue, and ensure existingFieldState missing does not cause a reset when the field already has a cleared current value. Keep the fix localized to useField so the mount-time path and the initial value path stay consistent.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Duplicate comments:
In `@src/useField.ts`:
- Around line 127-132: The pre-registration effect in useField is not using the
same value precedence as the initial value resolution, so a cleared current
value can be overwritten by formInitialValue before registration completes.
Update the effect that runs before registration to follow the same precedence as
the value logic around formInitialValue, formValue, and initialStateValue, and
ensure existingFieldState missing does not cause a reset when the field already
has a cleared current value. Keep the fix localized to useField so the
mount-time path and the initial value path stay consistent.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro
Run ID: 12e28802-8d05-4faf-af3d-4a769172cac8
📒 Files selected for processing (1)
src/useField.ts
Summary by CodeRabbit