dai-shi / react-tracked

State usage tracking with Proxies. Optimize re-renders for useState/useReducer, React Redux, Zustand and others.

Home Page:https://react-tracked.js.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unused stores lead to renders

andyyxw opened this issue · comments

commented

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!

image
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.
commented

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.