reselect README

The project's README from github.com/reduxjs/reselect. As one markdown file: README.md.

Reselect

npm packageCoverallsGitHub Workflow StatusTypeScript

A library for creating memoized "selector" functions. Commonly used with Redux, but usable with any plain JS immutable data as well.

The Redux docs usage page on Deriving Data with Selectors covers the purpose and motivation for selectors, why memoized selectors are useful, typical Reselect usage patterns, and using selectors with React-Redux.

Installation

Redux Toolkit

While Reselect is not exclusive to Redux, it is already included by default in the official Redux Toolkit package - no further installation needed.

import { createSelector } from '@reduxjs/toolkit'

Standalone

For standalone usage, install the reselect package:

# NPM
npm install reselect

# Yarn
yarn add reselect

# pnpm
pnpm add reselect

Documentation

The Reselect docs are available at https://reselect.js.org, and include usage guides and API references:

Basic Usage

Reselect exports a createSelector API, which generates memoized selector functions. createSelector accepts one or more input selectors, which extract values from arguments, and a result function function that receives the extracted values and should return a derived value. If the generated output selector is called multiple times, the output will only be recalculated when the extracted values have changed.

You can play around with the following example in this CodeSandbox:

import { createSelector } from 'reselect'

interface RootState {
  todos: { id: number; completed: boolean }[]
  alerts: { id: number; read: boolean }[]
}

const state: RootState = {
  todos: [
    { id: 0, completed: false },
    { id: 1, completed: true }
  ],
  alerts: [
    { id: 0, read: false },
    { id: 1, read: true }
  ]
}

const selectCompletedTodos = (state: RootState) => {
  console.log('selector ran')
  return state.todos.filter(todo => todo.completed === true)
}

selectCompletedTodos(state) // selector ran
selectCompletedTodos(state) // selector ran
selectCompletedTodos(state) // selector ran

const memoizedSelectCompletedTodos = createSelector(
  [(state: RootState) => state.todos],
  todos => {
    console.log('memoized selector ran')
    return todos.filter(todo => todo.completed === true)
  }
)

memoizedSelectCompletedTodos(state) // memoized selector ran
memoizedSelectCompletedTodos(state)
memoizedSelectCompletedTodos(state)

console.log(selectCompletedTodos(state) === selectCompletedTodos(state)) //=> false

console.log(
  memoizedSelectCompletedTodos(state) === memoizedSelectCompletedTodos(state)
) //=> true

As you can see from the example above, memoizedSelectCompletedTodos does not run the second or third time, but we still get the same return value as last time.

In addition to skipping unnecessary recalculations, memoizedSelectCompletedTodos returns the existing result reference if there is no recalculation. This is important for libraries like React-Redux or React that often rely on reference equality checks to optimize UI updates.


Terminology

The below example serves as a visual aid:

const outputSelector = createSelector(
  [inputSelector1, inputSelector2, inputSelector3], // synonymous with `dependencies`.
  resultFunc // Result function
)

What's New in 5.0.0?

Version 5.0.0 introduces several new features and improvements:

These updates aim to enhance flexibility, performance, and developer experience. For detailed usage and examples, refer to the updated documentation sections for each feature.

[ ↑ Back to top ↑ ]

License

MIT

References

Click to Expand

Originally inspired by getters in NuclearJS, subscriptions in re-frame and this proposal from speedskater.

reselect (reduxjs/reselect)

Searching Database…

这是什么?一个用来创建带记忆化(memoized)选择器函数的小型库,可以从你的 state 中派生出计算后的数据。它最出名的搭配是 Redux:让 store 只保留尽可能少的状态,不过它同样适用于任何普通的 JS 不可变数据。
它和 Redux 绑定吗,还是我可以在任何 state 上使用它?它是一个普通的 npm 包,不依赖 Redux——选择器可以处理任何普通的 JS 不可变数据。Redux Toolkit 默认已经内置了它,所以用 RTK 的人不需要额外安装。
我怎么把它加到项目里?如果你用的是 Redux Toolkit,什么都不用做——它已经包含在内了。否则单独安装这个包:npm install reselect。官方文档网站上有使用指南和完整的 API 参考。
我怎么发现选择器常见的错误?Reselect 自带仅用于开发环境的检查:inputStabilityCheck 会把输入选择器运行两次,如果同一次调用返回了不同的结果就发出警告;identityFunctionCheck 则会标记那些只是原样返回自己输入的结果函数。dependencyRecomputations 可以追踪输入的重新计算次数。
选择器返回相同的对象引用为什么很重要?React 和 React-Redux 靠引用相等性检查来优化更新。由于记忆化的选择器在数据没变时会直接返回现有的结果引用,连接到 store 的组件就能跳过不必要的重新渲染,而不是每次 store 事件都更新一遍。
5.0.0 版本有什么新变化?v5 把 weakMapMemoize 设为默认的记忆化器,要求 TypeScript 4.7 或更高版本,并移除了 ParametricSelector 类型以及 createStructuredSelector 的第二个重载。TypeScript 的性能也大幅提升:在遇到深层类型实例化错误之前,大约可以嵌套 30 个输出选择器,而以前只有 8 个左右。
Reselect 这个想法是从哪儿来的?README 中提到,最初的灵感来自 NuclearJS 的 getter、re-frame 的订阅机制,以及 speedskater 的一份提案。
Creator
reduxjs
Size
7.6 MB
Stars
19K
Age
11y
Commits · 90 Days