Idea for destructuring data instead of using many use*Store() calls #2759
Unanswered
mikeaustin
asked this question in
Ideas
Replies: 2 comments 3 replies
-
I don't dislike the idea, especially if const [foo, bar] = useSelectors(store, [
(state) => state.foo,
(state) => state.bar,
]); It's fine for a third-part library, but adding this to the zustand core seems going against the philosophy. |
Beta Was this translation helpful? Give feedback.
1 reply
-
I'm actually interested in comparing the performance difference. Would anyone like to give a shot? const store = createStore(() => ({ foo: 1, bar: 2 }));
// multiple hooks
const [foo, bar] = [
useStore(store, (state) => state.foo),
useStore(store, (state) => state.bar),
];
// useShallow
const [foo, bar] = useStore(store, useShallow((state) => [
state.foo,
state.bar,
]));
// useSelectors (no implementation yet)
const [foo, bar] = useSelectors(store, [
(state) => state.foo,
(state) => state.bar,
]); Of course, for the stress test, there should be dozens or hundreds of items, instead of just two. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
As I was reading how
useSelector()
works in Redux (and similar functionality in Zustand). I wondered if it would be possible to destructure data instead of having to use ause*Store()
for every piece of data.I prototyped a simple solution, where you provide all your selectors to
useStore()
:https://codepen.io/mikeaustin/pen/vYoOazz?editors=1010
Example
You can destructure your data without having to call so many functions:
In my prototype, reducers are standalone functions - no need to tie them to a particular store:
I wouldn't call it production ready, but it's dead simple. zustand was an inspiration. Thoughts?
Beta Was this translation helpful? Give feedback.
All reactions