Skip to content

useSnapshot not updating when using useMemo #301

Closed Answered by dai-shi
Elia-Darwish asked this question in Q&A
Discussion options

You must be logged in to vote

Yeah, proxy based tracking does not work well with React.memo.
https://github.com/pmndrs/valtio/wiki/Some-gotchas#using-reactmemo-with-object-props-may-result-in-unexpected-behavior

useMemo is the same too.

In your case, slices is an array which is wrapped with a proxy, so having that in useMemo prevents from correctly tracking the usage. This is very unfortunate. I ended up to educate about avoiding useMemo/memo with valtio.

Technically, we have an API for a solution https://github.com/dai-shi/proxy-compare/blob/main/src/index.ts#L276, but it's too hard to explain.

It is fine to use memo/useMemo for primitive values. So, the best is to destructure before using memo. Your use case can't d…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@Elia-Darwish
Comment options

@dai-shi
Comment options

Answer selected by Elia-Darwish
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants