Skip to content

[utils] Fix usePreviousValue equality comparison#5264

Open
lyzno1 wants to merge 1 commit into
mui:masterfrom
lyzno1:agent/fix-use-previous-value-equality
Open

[utils] Fix usePreviousValue equality comparison#5264
lyzno1 wants to merge 1 commit into
mui:masterfrom
lyzno1:agent/fix-use-previous-value-equality

Conversation

@lyzno1

@lyzno1 lyzno1 commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

Changes

  • compare values in usePreviousValue with Object.is
  • cover repeated NaN, transitions to NaN, and signed zero transitions

Why

usePreviousValue updates state during render when its input changes. Strict inequality treats NaN as different from itself, so rendering the hook with NaN repeatedly schedules another render and eventually throws a "Too many re-renders" error.

Using Object.is makes the hook follow React's state equality semantics: repeated NaN values are stable, while positive and negative zero remain distinguishable.

Impact

Consumers of the public utility can safely pass NaN without triggering an infinite render loop. Existing internal call sites use non-numeric values and retain their current behavior.

Tests

@lyzno1
lyzno1 marked this pull request as ready for review July 18, 2026 12:49
@pkg-pr-new

pkg-pr-new Bot commented Jul 18, 2026

Copy link
Copy Markdown

commit: 9c678bd

@code-infra-dashboard

code-infra-dashboard Bot commented Jul 18, 2026

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react 🔺+9B(0.00%) ▼-2B(0.00%)

Details of bundle changes

Performance

Total duration: 1,039.53 ms -71.67 ms(-6.5%) | Renders: 78 (+0) | Paint: 1,648.10 ms -118.36 ms(-6.7%)

Test Duration Renders
Tooltip mount (300 contained roots) 40.89 ms ▼-11.21 ms(-21.5%) 1 (+0)

14 tests within noise — details


Check out the code infra dashboard for more information about this PR.

@netlify

netlify Bot commented Jul 18, 2026

Copy link
Copy Markdown

Deploy Preview for base-ui ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 9c678bd
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6a5b75f77eff6d0007c1b44b
😎 Deploy Preview https://deploy-preview-5264--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@zannager zannager added the package: utils Specific to the utils package. label Jul 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

package: utils Specific to the utils package.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants