Selectors vs destructuring #1045
-
Hi When browsing the documentation I see a lot of references to selectors and third-party plugins to automatically generate them. From what I've gathered from the documentation, selectors are "recommended to use when using either the properties or actions from the store". My question is, what's the difference between using the selectors vs. simply destructuring from the store? Destructuring: const { count } = useStore() Selectors: const count = useStore(state => state.count) Given that the ecosystem has written selector generators, I must be missing something obvious? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Hi, it's very different if you have isolated This 👇 will trigger re-render if const { count } = useStore() This 👇 will not trigger re-render if const count = useStore(state => state.count)
https://github.com/pmndrs/zustand#fetching-everything says "bear in mind that it will cause the component to update on every state change!" |
Beta Was this translation helpful? Give feedback.
-
btw, you might be interested in trying valtio. https://github.com/pmndrs/valtio. |
Beta Was this translation helpful? Give feedback.
Hi, it's very different if you have isolated
anotherCount
property in your store.This 👇 will trigger re-render if
anotherCount
changes.This 👇 will not trigger re-render if
anotherCount
changes.https://github.com/pmndrs/zustand#fetching-everything says "bear in mind that it will cause the component to update on every state change!"
Any suggestion to add a note there?