Feature Request: Define no-op behavior for empty object partial updates in setState #3329
Replies: 1 comment
-
|
Official workaround: |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Bug Description
Problem Description
Some applications call setState defensively with an empty object ({}), for example when update logic is conditional or composed dynamically.
Currently, there is no explicit contract guaranteeing that calling:
store.setState({})
is treated as a no-op. In practice, this can lead to:
Unexpected subscriber notifications
Unnecessary state identity changes
Subtle performance issues in real-world apps
This feature request proposes a clear and explicit behavior contract for this case.
### Proposed Behavior
When calling setState(partial, replace?):
✅ New Contract (Feature Request)
If partial is an object with no enumerable own keys
And replace is not true
Then:
The update must be treated as a no-op
The store must not notify subscribers
The store must preserve the existing state reference
🚫 Explicitly Out of Scope
This applies only to empty object partial updates ({})
Existing semantics for:
Non-empty partials
Updater functions that return keys
replace: true
must remain unchanged.
Why This Matters
Defensive setState({}) calls are common in application code
Treating empty partials as no-ops avoids unnecessary updates
Makes state updates more predictable and easier to reason about
Aligns with developer intuition: “no keys” → “no change”
Test Coverage / Specification
A comprehensive test suite is provided to define the exact expected behavior, covering:
No notifications for empty object partials
No state identity changes for empty partials
Notifications for:
Non-empty partials (even if values are identical)
Symbol keys
undefined, NaN, -0 / 0
Function references
Preservation of replace: true semantics
Correct subscriber behavior
Test Environment
Package manager: pnpm
Test runner: vitest
Scope: vanilla store
Relevant test file:
tests/vanilla/noop-partial-updates.test.ts
Expected Outcome
By defining this behavior explicitly, Zustand can:
Prevent accidental regressions
Improve performance characteristics
Provide a clearer mental model for setState
Support common defensive update patterns safely
Reproduction Link
https://shipd.ai/dashboard/20265bcf-97be-4acb-a007-812bcabc8b93
Beta Was this translation helpful? Give feedback.
All reactions