Skip to content

How to Get Computed State without Infinite Loop #2979

Answered by dai-shi
mdavish asked this question in Q&A

You must be logged in to vote

In Zustand 4, option 1 seemed to work just fine.

It's a misunderstanding. Zustand 4 doesn't optimize rerenders in that case. Zustand 5 reveals the issue that was hidden in Zustand 4.

So my question is - what is the best practice for computed state in Zustand version 5?

The best practice with vanilla Zustand regardless of its version is a custom hook, which technically means option 5.

Option 5 feels a bit verbose and not very reusable.

There are some middlewares in the ecosystem such as https://github.com/cmlarsen/zustand-middleware-computed-state, but I'm not sure how much they are used.

Replies: 1 comment 4 replies

You must be logged in to vote
4 replies
@mdavish

@dai-shi

@mdavish

@dai-shi

Answer selected by mdavish
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