-
https://github.com/pmndrs/eslint-plugin-valtio#snapshots-in-callbacks-are-not-recommended would love to get a better explanation. thanks |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 9 replies
-
Thanks for raising this up. const state = proxy({ count: 0, text: '' })
function App() {
const snap = useSnapshot(state)
const handleClick = () => {
console.log(snap.text) // This is not recommended as it can cause extra re-renders
}
return (
<div>
{snap.count} <button onClick={handleClick}>click</button>
</div>
)
} Once, you click the button, the |
Beta Was this translation helpful? Give feedback.
Thanks for raising this up.
Yeah, the problem is not reading stale value, but it's about state usage tracking.
Once, you click the button, the
useSnapshot
hook can mistake thattext
is used in render, which may cause re-render iftext
changes.snap
is really meant to be used only in render function.