JiangangXiong
JiangangXiong

Reputation: 2426

Will the component re-rende if selectors change third time?

For this selector code:

import { createSelector } from 'reselect'
import { FilterTypes } from '../constants'

export const selectFilterTodos = createSelector(
[getTodos, getFilters],
(todos, filters) => {
  switch(filters) {
    case FilterTypes.ALL:
        return todos;
    case FilterTypes.COMPLETED:
        return todos.filter((todo) => todo.completed)
    default:
        return todos
  }
}
)

If the redux state change from state one to state two and then return state one, how many times will the selector’s second function param run? Maybe it‘s twice,and then will you want the selector to reuse the memorized result when state two to state one?

Upvotes: 0

Views: 44

Answers (1)

markerikson
markerikson

Reputation: 67459

Reselect's createSelector has a default memoization size of 1. So, if you call a selector with:

someSelector(state, "a");
someSelector(state, "b");
someSelector(state, "a");

It will re-run the "output selector" 3 times. The first time there is no cached value, and the second and third times the inputs are different, so it will not use the cached value.

If you need to change the memoization behavior, you can use createSelectorCreator to customize the comparisons and the caching.

Upvotes: 1

Related Questions