Skip to content

how does useSelector cache without re-running the selector ? #1892

Answered by markerikson
ethan-cao asked this question in Q&A
Discussion options

You must be logged in to vote

It'll also re-run if the state changed as well:

if (
selector !== latestSelector.current ||
storeState !== latestStoreState.current ||
latestSubscriptionCallbackError.current
) {
const newSelectedState = selector(storeState)
// ensure latest selected state is reused so that a custom equality function can result in identical references
if (
latestSelectedState.current === undefined ||
!equalityFn(newSelectedState, latestSelectedState.current)
) {
selectedState = newSelectedState
} else {
selectedState = latestSelectedState.current
}
} else {

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@ethan-cao
Comment options

@ethan-cao
Comment options

@markerikson
Comment options

Answer selected by ethan-cao
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