If I use actions as a key in store it is stripped out from it for subsequent renders
#3398
Replies: 2 comments
-
|
@alex-bondarev-linnovate would you mind creating a demo on stackblitz? |
Beta Was this translation helpful? Give feedback.
-
|
The Most likely: If you are using the Quick check: remove Secondary: infinite re-render from reference inequality Even without devtools, selecting Fix // Option 1: shallow equality (works with both scenarios)
import { shallow } from 'zustand/shallow'
export const useBearActions = () =>
useBearStore((state) => state.actions, shallow)
// Option 2: select individual actions (stable function references)
export const useAddBear = () => useBearStore((state) => state.actions.addBear)
export const useRemoveBear = () => useBearStore((state) => state.actions.removeBear)
// Option 3: read actions outside React (they never change)
const { addBear, removeBear } = useBearStore.getState().actionsOption 1 is the drop-in fix with minimal change. Option 2 or 3 are better if you only need specific actions in a given component, since they avoid creating a selector that returns an object at all. If you are on zustand v5, |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
A bit weird but if I follow TkDodo's advice on separating actions from state and create a hook for it, much like in the example
export const useBearActions = () => useBearStore((state) => state.actions);it fails on subsequent rerenders. So it works fine on initial render but then errors. All other 'dedicated' hooks for the same store work fine even in the same component. I've tripple-checked the syntax.
What is even weirder is that if I change
actionsto something else it works just fine. Has anybody experienced anything like it? I'm pretty sure that the issue is with me and how I do stuff, but still 😃 I've never experienced receiving half of the store in a component simply because I chose the wrong key inside of it (so it seems). I'm receiving everything but the 'actions' bit.Beta Was this translation helpful? Give feedback.
All reactions