Best ways to avoid unnecessary rerenders with zustand are :
Direct selectors:
const amount = useBookStore(state => state.amount) const updateAmount = useBookStore(state => state.updateAmount)
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.
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) :