Skip to content

When do selectors recalculate? #4267

Answered by david-shortman
anno1985 asked this question in Q&A
Discussion options

You must be logged in to vote

The inputs for selectors are determined when the state changes. The inputs are compared by reference for objects, and by value for primitives.

Selectors don't have to be recalculated if selectors are composed in small slices that can be memoized.

By looking at a simplified implementation of how createSelector and store.select work, then you'll see how selectors get updates.

createSelector is a function that simply creates another function that takes in state and returns a value. Here's a simplified example:

function createSelector(...otherSelectors, projector) {
  // Define an internal state for memoization
  let lastMemoizedArguments: Array<unknown> = undefined;
  let lastMemoizedResult: u…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@david-shortman
Comment options

Answer selected by anno1985
Comment options

You must be logged in to vote
0 replies
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