isnt this a better way to get the properties from the store?
const {amount, updateAmount} = useBookStore()
instead of
const amount = useBookStore(state => state.amount) const updateAmount = useBookStore(state => state.updateAmount)
no if any other changes occur in store other than amount and updateAmount , you will have unnecessary rerender .
Oh wow, had no clue. Thanks for pointing that out! I guess it would still work with smaller and specific stores though, but i'll keep it in mind.
This isn't true.
Can you elaborate?
Best ways to avoid unnecessary rerenders with zustand are :
Direct selectors:
Object destructuring with shallow:
shallow
import { shallow } from "zustand/shallow"; const { amount } = useBookStore((state) => ({ amount: state.amount, }), shallow);
Since v4.x.x, you can also use the useShallow hook:
useShallow
import { useShallow } from 'zustand/react/shallow' const { amount } = useBookStore( useShallow((state) => ({ amount: state.amount })), );
Other ways are wrong (unless re-renders / performance is not a problem) :
const state = useBookStore();
const { amount } = useBookStore();
const { amount } = useBookStore((state) => ({ amount: state.amount, }});
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
isnt this a better way to get the properties from the store?
instead of
no
if any other changes occur in store other than amount and updateAmount , you will have unnecessary rerender .
Oh wow, had no clue. Thanks for pointing that out!
I guess it would still work with smaller and specific stores though, but i'll keep it in mind.
This isn't true.
Can you elaborate?
Best ways to avoid unnecessary rerenders with zustand are :
Direct selectors:
Object destructuring with
shallow:Since v4.x.x, you can also use the
useShallowhook:Other ways are wrong (unless re-renders / performance is not a problem) :