Comments (6)
I believe it's the same in Redux, subs will fire for any dispatch?
We probably could just compare the root in order to detect reduced state changes, right?
const setState = (partialState: PartialState<State>) => {
const oldState = state
state = Object.assign(
{},
state,
typeof partialState === 'function' ? partialState(state) : partialState
)
if (oldState !== state) listeners.forEach(listener => listener(state))
}
edit:
On second thought, ... assign creates a new object each time, so that probably wouldn't work. π€If root change detection can get expensive i would just accept the current behaviour, and the upcoming selective subscribe will also help.
from zustand.
I have a fix that's basically the same but does a shallow equal check. I just wanted to open this issue to see if you had any more info on what the behavior should be and to reference this for a potential fix. I'll check if redux is the same.
from zustand.
The selective api.subscribe
change is how I discovered this. The behavior seemed inconsistent because a selective subscription would not be called unless its state changed but a non-selective one would be called every time even on no change.
from zustand.
just scared that adding a shallow equal check will add overhead (for instance root objects with hundredes/thousands of objects in it, like a central-state hashmap). At work our state is formed like that, there can be thousands of object in state-root. It would run over each on every setState.
One way: subscribe(fn) could expectedly fire on each setState, with docs declaring it so.
Or, maybe, ... is it possible to run the check only over the partialState?
const partial = typeof partialState === 'function' ? partialState(state) : partialState
// This works if "shallowEqual" could disregard that A has less keys than B somehow
if (shallowEqual(partial, state)) {
state = Object.assign({}, state, partial)
from zustand.
Yeah, we can run the check only on the keys in partialState:
const setState: SetState<TState> = partial => {
const partialState =
typeof partial === 'function' ? partial(state) : partial
if (
partialState !== state &&
Object.entries(partialState).some(
([key, value]) => !Object.is(state[key], value)
)
) {
state = Object.assign({}, state, partialState)
listeners.forEach(listener => listener(state))
}
}
from zustand.
I kept thinking about this, sadly this doesn't help when the user does set(state => ({ ...state, something: 123 }))
from zustand.
Related Issues (20)
- Reset store not working on log out
- I can't update state with new data from Tanstack Query HOT 2
- The app hanging when initializing the store with and array of objects. HOT 4
- Is there any chance to simplify types?
- How to move the router state to store?
- Website graphics rendering issues (Linux) HOT 3
- shallow not support nested object HOT 1
- Are these two methods exactly the same? HOT 1
- Itβs not recommended to suspend a render based on a store value returned by useSyncExternalStore.
- RFC: How is v5-alpha? HOT 11
- should useShallow add into the documents?
- Can't use persist with context implementation mentioned in the docs for Next.js HOT 1
- Re-render issue while performing open and setOpen using shadcn
- Re-render issue while performing open and setOpen using shadcn HOT 1
- type WithoutCallSignature<T>
- docs: Add suggestions about getInitialState on persist
- Cant user Array of Map type to store data
- overview of the zustand API HOT 1
- Persist storage : values are converted to strings before replacer
- Value is empty on initial render and sets after few milliseconds
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from zustand.