简体   繁体   English

React Hooks:有条件地渲染一个组件,但可以访问整个 state

[英]React Hooks: conditionally render a component but with access to the whole state

Let's say my state is represented by an object like that:假设我的 state 由 object 表示,如下所示:

state = {
    array: [1, 2, 3, 4, 5]
}

I know if i use:我知道我是否使用:

const state = useSelector(state => state.array)

My component re-render every time one of the elements of the array changes, but i don't want it.每次数组的元素之一发生更改时,我的组件都会重新渲染,但我不想要它。 So, a solution can be:因此,解决方案可以是:

state = {
    render: false,
    array: [1, 2, 3, 4, 5]
}

And now i can:现在我可以:

const state = useSelector(state => state.render)

And handle the rendering by conditionally dispatching some actions.并通过有条件地调度一些动作来处理渲染。 But in this way i have no access to the array anymore.但是这样我就无法再访问数组了。 The problem here is that in my project the are components who need to re-render every time the array updates, and components who need to re-render only in specific cases.这里的问题是,在我的项目中,每次数组更新时都需要重新渲染的组件,以及仅在特定情况下需要重新渲染的组件。 Another solution is to create another store only for the component which need to re-render rarely, but it seems too wired to me.另一种解决方案是只为很少需要重新渲染的组件创建另一个商店,但这对我来说似乎太有线了。

useSelector accepts a second argument equlityFn : useSelector接受第二个参数equlityFn

const result: any = useSelector(selector: Function, equalityFn?: Function)

If equlityFn returns true , no subscription update will accurate, so the render won't result from the selector.如果equlityFn返回true ,订阅更新将不准确,因此渲染不会来自选择器。

const state = useSelector(
  (state) => state.array,
  (prevState, currState) => {
    return isRender(currState);
  }
);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有条件地调用 React Hook “useCustomHook”。 在每个组件渲染中,必须以完全相同的顺序调用 React Hooks - React Hook "useCustomHook" is called conditionally. React Hooks must be called in the exact same order in every component render React Hook“useEffect”被有条件地调用。 React Hooks 必须在每个组件渲染中以完全相同的顺序调用 - React Hook "useEffect" is called conditionally. React Hooks must be called in the exact same order in every component render 使用 React Hooks 卸载组件时如何访问状态? - How to access state when component unmount with React Hooks? useNameProps" 被有条件地调用。React Hooks 必须在每个组件渲染中以完全相同的顺序调用 - useNameProps" is called conditionally. React Hooks must be called in the exact same order in every component render 如何使用反应挂钩有条件地重置 state 计数 - How to conditionally reset state count with react hooks React HOC 有条件地渲染组件 - React HOC to conditionally render a component 用钩子和状态反应纯组件 - React pure component with hooks and state React Hook "useState" 被有条件地调用。 在每个组件渲染错误中,必须以完全相同的顺序调用 React Hooks - React Hook "useState" is called conditionally. React Hooks must be called in the exact same order in every component render error 错误:有条件地调用 React Hook “useCallback”。 在每个组件渲染中,必须以完全相同的顺序调用 React Hooks - Error: React Hook "useCallback" is called conditionally. React Hooks must be called in the exact same order in every component render 设置新的 state 不会使用 React Hooks 重新渲染组件 - Setting new state won't re-render the component with React Hooks
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM