How to prevent unnecessary re-renders when a snapshot is not used? #867
Unanswered
pastelmind
asked this question in
Q&A
Replies: 1 comment 2 replies
-
You should create another component in this case for optimization: function ShowFoo(props) {
if (props.isDisabled) {
return <div>Show nothing</div>
}
return <ShowFooInternal />
}
function ShowFooInternal() {
const snapshot = useSnapshot(store)
return <div>foo: {snapshot.foo}</div>
} |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Suppose I have a store with two properties, and a component that conditionally depends on one of them
When
props.isDisabled
is falsy,<ShowFoo>
depends onstore.foo
only. It does not re-render whenstore.bar
changes. However, whenprops.isDisabled
is truthy, it re-renders whenstore.bar
changes, too. In fact,<ShowFoo>
now depends on the entirety ofstore
.Here is a reproduction: https://codesandbox.io/p/sandbox/valtio-unnecessary-rerenders-ylkq89
This is causing performance issues in my codebase. Is there a workaround for it?
Beta Was this translation helpful? Give feedback.
All reactions