Skip to content

Comments

fix(deepEqual): handle NaN comparison correctly using Object.is#13120

Merged
bluebill1049 merged 2 commits intoreact-hook-form:masterfrom
kamja44:fix/deepEqual-nan-comparison
Nov 2, 2025
Merged

fix(deepEqual): handle NaN comparison correctly using Object.is#13120
bluebill1049 merged 2 commits intoreact-hook-form:masterfrom
kamja44:fix/deepEqual-nan-comparison

Conversation

@kamja44
Copy link
Contributor

@kamja44 kamja44 commented Nov 2, 2025

The deepEqual function uses the === operator for primitive value comparison. In JavaScript, NaN === NaN always returns false, causing incorrect NaN comparisons.

Potential Issues

  • isDirty malfunction: Fields with empty number inputs (NaN) using valueAsNumber are marked as dirty even when unchanged
  • dirtyFields errors: Fields containing NaN values are always marked as dirty
  • reset() issues: Fields with NaN default values remain dirty after reset

Solution

  • Replace === operator with Object.is()
  • Object.is(NaN, NaN) returns true, enabling correct comparison
  • Add comprehensive tests for NaN comparison scenarios

kamja44 and others added 2 commits November 2, 2025 12:57
- Replace === with Object.is() for primitive comparison
- Add comprehensive tests for NaN in objects, arrays, and nested structures
- Fixes incorrect isDirty behavior when using valueAsNumber with empty inputs
@bluebill1049 bluebill1049 merged commit 4922698 into react-hook-form:master Nov 2, 2025
6 checks passed
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