React 在用户空间中的 useContextSelector 钩子
React 用户态中的 useContextSelector Hook
[!IMPORTANT] 本库的目标是模拟 React Context API 在并发 React 下的行为。 许多用户尝试使用本库来避免不必要的重新渲染,而无需考虑并发 React。 如果您只是想避免重新渲染,我们推荐以下方案之一:
React Context 和 useContext 常被用来避免 prop drilling, 但众所周知,这存在性能问题。 当 context 值发生变化时,所有使用 useContext 的组件都会重新渲染。
为了解决这个问题,
提出了 useContextSelector,随后又提出了
支持 context selector 的 useContext。
这个库在用户空间中提供了这些 API。
在 v1.3 之前,它使用 changedBits=0 特性来阻止传播,
v1.3 不再依赖这个未文档化的特性。
此包需要一些 peer dependencies,你需要自行安装。
npm install use-context-selector react scheduler给库作者的说明:
请务必保留 "peerDependencies",并添加说明,指导用户安装 peer dependencies。
为了使其像原始的 React 上下文一样工作,它有意使用了 useReducer 作弊模式。
它还需要 useContextUpdate 才能在并发渲染中表现更好。
其使用是可选的,仅当默认行为不符合预期时才需要。
…这会为 useContextSelector 创建一个特殊的上下文。
defaultValue Value import { createContext } from 'use-context-select暂无开放 Issues,或尚未同步最近议题。