Skip to content

Does pattern changed about using selector in component? #971

Answered by dai-shi
seanbruce asked this question in General
Discussion options

You must be logged in to vote

Thanks for opening a discussion.

In the past, we recommended the use of stable selectors in v3, because we were targeting v4 with useMutableSource at that point.

After that, React deprecated experimental useMutableSource and provides useSyncExternalStore.
For more context:

  1. https://twitter.com/TkDodo/status/1427922647401312257
  2. reactwg/react-18#84
  3. reactwg/react-18#86

Now, zustand v4 is based on useSyncExternalStore, so stable selectors are not necessary. But if you have a stable selector, you can expect the performance is a little more improved.

In summary:

inline selectors are just fine and recommended

const Component = () => {
  const slice = useStore((state) => state.slice);
  // ...

u…

Replies: 1 comment 8 replies

Comment options

You must be logged in to vote
8 replies
@pastelmind
Comment options

@dai-shi
Comment options

@pastelmind
Comment options

@pastelmind
Comment options

@dai-shi
Comment options

Answer selected by seanbruce
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants