Skip to content

Best practices on using selectors in v5 #2867

Answered by dbritto-dev
nkleidis asked this question in General
Discussion options

You must be logged in to vote

@nkleidis instead of stable selector we need to talk about stable selector results, you could have a stable selector that does not return stable results

// Case 1: stable selector result
const prop = useStore(state => state.prop);

// Case 2: stable selector result
const pets = useStore(state => state.pets); // We can use `useShallow` like this `useStore(useShallow(state => state.pets))`
const petNames = pets.map(pet => pet.name)
// Note: you can put this in a custom hook called `usePetNames`

// Case 3: stable selector result
const pets  = useStore(state => state.pets);  // We can use `useShallow` like this `useStore(useShallow(state => state.pets))`
const petsWithExtraInfo = pets.map(pet 

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@nkleidis
Comment options

@dbritto-dev
Comment options

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