Search code examples
reactjsreduxreact-reduxredux-toolkit

createSelector not memoize


Try understand how to work createSelector

I wrote a small example: My example

Why is it that when I update the state unrelated (push button Change counter) to the createSelector arguments, a callback occurs(console output "calculation")?


Solution

  • Yes, because you are calling createSelector every time this component renders. That means that it's a new selector instance every time.

    Memoization only works if you create a selector instance once, and then reuse that selector each time.

    Move the selectA/B/C functions and const selectABC = createSelector() to be outside of this component, and it should work.

    I'd also suggest reading through the Deriving Data with Selectors page in the Redux docs.