About best practice, using it in project the first time, I've written tons of boilerplate, wondering if it can be more concise? #754
-
For instance my App component needs to get access to a number of states and methods, I wrote them one by one like this in App: const els = useStore((state) => state.els);
const setEls = useStore((state) => state.setEls);
const setLabelEditing = useStore((state) => state.setLabelEditing);
const showTool = useStore((state) => state.showTool);
const showEditor = useStore((state) => state.showEditor);
const editorId = useStore((state) => state.editorId);
const editElById = useStore((state) => state.editElById); Is this the best practice? If not what is? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
That looks good to me. Alternatively, you could combine them like in an array and use If you are looking for another practice, we can define actions outside useStore in advance. |
Beta Was this translation helpful? Give feedback.
-
@dai-shi Thanks. By the way what's the best way, performance wise, to do a selector? I'd imaging if I do that, any change to |
Beta Was this translation helpful? Give feedback.
-
In this article the author uses this pattern: const { count, increment, decrement } = useCounterStore(); I haven't seen this anywhere else but it works for me and saves so mcuh boilerplate! Is there any issues/downsides with using this approach over OP's code? |
Beta Was this translation helpful? Give feedback.
That looks good to me. Alternatively, you could combine them like in an array and use
shallow
.If you are looking for another practice, we can define actions outside useStore in advance.
See: https://github.com/pmndrs/zustand/wiki/Practice-with-no-store-actions