简体   繁体   English

防止React重新呈现特定的子组件

[英]Prevent React from re-rendering a specific child component

I have a react component that conditionally renders several child components. 我有一个有条件地渲染几个子组件的react组件。 Simplified code is just: 简化的代码就是:

render(): {
  const component1 = condition ? renderComponent2() : null;
  const component2 = renderComponent2();

  return (
    <div>
      {component1}
      {component2}
    </div>
  );
}

The problem is that component2 is getting destroyed and re-rendered whenever the condition changes. 问题是,只要condition发生变化, component2就会被破坏并重新呈现。 I'm trying to prevent that and keep the original element around. 我试图阻止它并保持原始元素。 I tried adding a key to component2 with no luck. 我尝试在没有运气的情况下向component2添加key

[edit] This is happening even when component1 always exists and I change flag on it to hide it or not with CSS :/ [edit]即使在component1总是存在的情况下也会发生这种情况,并且我在其上更改标志以使用CSS隐藏它:/

Form the example code, your component2 will not be destroyed and re-mounted again. 形成示例代码,您的component2将不会被销毁并再次重新安装。 React will run any render and possibly other lifecycle methods, but React will Update the component in the DOM in place. React将运行任何render和可能的其他生命周期方法,但React将更新 DOM中的组件。

Maybe your code is more complicated. 也许你的代码更复杂。 Which causes react to think that you are not re-rendering component2 , but instead, are trying to render a whole new component. 这会导致您认为自己没有重新渲染component2 ,而是尝试渲染一个全新的组件。 But again, from your example code this is not clear. 但同样,从您的示例代码中可以看出这一点并不清楚。

You can find proof of concept codepen here , which does the following: 您可以在此处找到概念证书代码库 ,其中包含以下内容:

  • It renders 2 instances of component2 , with a green background. 它呈现了2个具有绿色背景的component2实例。
  • Button can ( illegally ) change background color of the first component to red, outside of react's knowledge. 按钮可以( 非法 )将第一个组件的背景颜色更改为红色,而不是反应的知识。
  • By clicking the button, react will re-render the 2 components. 通过单击按钮,react将重新呈现2个组件。
  • The background-color remains red, which is proof that react only updates component, and does not destroy. 背景颜色保持红色,这证明反应只更新组件,并且不会破坏。

React will NOT reset the background color to green, because react thinks (from its virtual DOM) that the background color is unchanged and still green. React不会将背景颜色重置为绿色,因为react认为(来自其虚拟DOM)背景颜色未更改且仍为绿色。

UPDATE: The codepen now also contains further proof of how it CAN happen: 更新:codepen现在还包含有关它如何发生的进一步证明:

  • if you change the type of HTML returned (from <p> element to <h1> element in proof of concept) 如果更改返回的HTML类型(从概念证明中的<p>元素到<h1>元素)
  • react will NOT recognize it as the same element, and destroy original and insert a new element. react将不会将其识别为相同的元素,并销毁原始元素并插入新元素。

PS: because your example code creates the component through a method call, any lifecycle methods (such as shouldComponentUpdate ) should NOT be applied. PS:因为您的示例代码通过方法调用创建组件,所以不应该应用任何生命周期方法(例如shouldComponentUpdate )。 Rendering components through methods should only be done for simple components, ie react elements. 通过方法渲染组件只应针对简单组件,即反应元素。 See official docs here : 请参阅此处的官方文档

A ReactElement is a light, stateless , immutable, virtual representation of a DOM Element. ReactElement是DOM元素的轻量级, 无状态 ,不可变的虚拟表示。

Have you tried doing it with shouldComponentUpdate ? 您是否尝试过使用shouldComponentUpdate This is exactly what this function should be used for. 这正是应该使用此功能的原因。 Just add it to your component2 and add proper condition inside. 只需将其添加到您的component2并在其中添加适当的条件。

如果condition状态发生变化,组件将自行重新渲染,这意味着component2也将被更改。

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

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