简体   繁体   English

即使我们只需要渲染一个组件,所有组件都将在react-redux中渲染

[英]All component gets rendered even if we required to render only one, in react-redux

I'm not sure it is default behaviour or redux or something else but i found that on dispatching an action, this action traverse through all reducers(that's ok) but it also invoke the connect listener of every reducer that further resulting rendering of its component. 我不确定这是默认行为还是redux或其他东西,但是我发现在分派动作时,该动作会遍历所有reducer(没关系),但它还会调用每个reducer的connect侦听器,从而进一步导致其组件的呈现。 This means on every dispatch, all component inside app state tree get rendered. 这意味着在每次调度中,应用状态树中的所有组件都会被渲染。 Is this intentionally done by redux or i've done something wronge. 是由redux故意完成的,还是我做错了什么。 Help me out to clarify this things. 帮我弄清楚这件事。

In Redux , your state is global and handled by the redux, whenever you dispatch an action , you are just setting the global state. 在Redux中,您的状态是全局的,并由redux处理,每当您分派操作时,您都只是在设置全局状态。 Your container comonents will receive the new state and reducer would work on them but your components wont be rerendered since previous state and next state would be same. 您的容器组件将收到新状态,而reducer可以在其上工作,但是您的组件将不会重新渲染,因为上一个状态和下一个状态相同。

Only those components would be rendered whose mapStatetoProps result in a different result 仅渲染那些mapStatetoProps导致不同结果的组件

This behavior is totally fine. 这种行为是完全可以的。 See the React Docs for their virtual DOM concept: 有关虚拟DOM概念,请参见React Docs

React makes use of a virtual DOM , which is a descriptor of a DOM subtree rendered in the browser. React使用虚拟DOM ,它是浏览器中呈现的DOM子树的描述符。 This parallel representation allows React to avoid creating DOM nodes and accessing existing ones, which is slower than operations on JavaScript objects. 这种并行表示允许React避免创建DOM节点和访问现有的DOM节点,这比对JavaScript对象的操作要慢。 When a component's props or state change, React decides whether an actual DOM update is necessary by constructing a new virtual DOM and comparing it to the old one. 当组件的属性或状态发生变化时,React通过构造一个新的虚拟DOM并将其与旧的DOM进行比较,来决定是否需要进行实际的DOM更新。 Only in the case they are not equal, will React reconcile the DOM, applying as few mutations as possible. 只有在它们不相等的情况下,React才会协调DOM,并应用尽可能少的突变。

So you don't have to worry that every component will get re-rendered every time you dispatch an action. 因此,您不必担心每次派发操作时都会重新渲染每个组件。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM