Comments (8)
Indeed, this has to work.
@JeremyRH It stores the selector as a ref and runs it each render pass over the store-state. Since the function is exchanged, shouldn't it be able to take the updated scope?
from zustand.
Playing around some more, the store is never updated since nothing in it changes, so it doesn't fire subscribers. setId(id => id + 1) just returns arg0, but doesn't call set. Hence the selector is called only once, initially. Isn't this expected, though?
from zustand.
On second thought, you're right, when the selector changes it can yield another result, so it should definitively be possible to run a diff even though the store hasn't been changed.
from zustand.
Exactly. We should either add back dependencies
or be more restrictive how the selecting can happen. Because if I can write code like this to select state Iβm expecting it to work.
from zustand.
Like this maybe? https://codesandbox.io/s/o7nky5wwj5
This would also work if you remove the [id], in that case it would call the selector on every render, more or less like redux works. If you supply the selector, it runs only on memoize changes. I haven't tested if this causes extra render calls now.
from zustand.
Yeah, you bring up extra renders... I worry that the component will render, triggering the React.useEffect(() => void ping(), deps)
which in turn will trigger another render.
from zustand.
I think this seems like a pretty good solution, barring it doesn't cause a bunch of extra renders...
from zustand.
I think we can solve this issue without adding the dependencies back. We can run the selector on every call to useStore
and return that slice instead of returning the state from React's useState
. I'm not sure if this will cause unwanted side effects though. I'll test this out today.
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.