Unused stores lead to renders
andyyxw opened this issue · comments
I use useTrackedStore
returned by createTrackedSelector(useStore)
with zustand
, there are no properties of store used in the current component, but any changes in the store will result in a render of the component.
Please help me understand this behaviour, thanks!
https://codesandbox.io/s/react-typescript-forked-l6k6lv?file=/src/App.tsx:1049-1064
This is an intended behavior by design.
If you don't touch a property of an object, the library assumes that the whole object is "used".
const state = useTrackedStore();
// The whole `state` is marked as used, if you don't use `state` properties.
const { obj } = useTrackedStore();
// In this case, `obj` is marked as used, if you don't use `obj` properties.
This is an intended behavior by design. If you don't touch a property of an object, the library assumes that the whole object is "used".
const state = useTrackedStore(); // The whole `state` is marked as used, if you don't use `state` properties. const { obj } = useTrackedStore(); // In this case, `obj` is marked as used, if you don't use `obj` properties.
@dai-shi Thanks, I've understood it. Can you tell me the code related to this behaviour, is it in proxy-compare
?
Yeah, it's proxy-compare.