简体   繁体   English

如何在父组件的道具发生变化时停止重新渲染子组件?

[英]How to stop re-rendering of child component when its parent's props change?

I have a Modal that is a child of a Card component:我有一个ModalCard组件的子组件:

Card > Modal

The Card has props that it sends down to the Modal . Card有它发送到Modal的道具。 When I select an option in the Modal 's UI, it updates the data in the Card and, thus, re-renders the Card , closing the Modal as it does so.当我 select Modal的 UI 中的一个选项时,它会更新Card中的数据,从而重新呈现Card ,同时关闭Modal

This is not ideal because I would like the user to continue to use the Modal until they explicitly hit "close".这并不理想,因为我希望用户继续使用Modal ,直到他们明确点击“关闭”。

Is there a way to update the data/state in the underlying Card (so that the Modal can use the new state), but NOT trigger a full re-render of the component tree until the Modal is finally closed?有没有办法更新底层Card中的数据/状态(以便Modal可以使用新状态),但在Modal最终关闭之前不会触发组件树的完全重新渲染?

There is no Redux involved right now, and I'd like to avoid it if possible.现在没有涉及 Redux,如果可能的话我想避免它。

Whenever a state is updated, all components that use the state and its children are re-rendered.每当 state 更新时,所有使用 state 及其子组件的组件都会重新呈现。 You should probably use react portals to achieve what you need with the modal.您可能应该使用反应门户来实现您对模态的需求。 There is a fiddle on the end of the docs link.文档链接的末尾有一个小提琴。 Docs: https://reactjs.org/docs/portals.html文档: https://reactjs.org/docs/portals.html

暂无
暂无

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

相关问题 如何在父组件重新加载时阻止子组件重新渲染 - How do I stop a child component from re-rendering when its parent reloads 当道具更改时,React 子组件不会重新渲染 - React Child Component not re-rendering when props change 将 props 传递给子组件时避免组件重新渲染 - Avoid component re-rendering when passing props to child component 父组件在父状态更改时不必要地重新渲染子级 - Parent component unnecessarily re-rendering child on parent state change React:当只有子组件需要重新渲染时,如何防止父组件重新渲染 onmousemove 状态更改? - React: How can I prevent the parent component re-rendering onmousemove state change when only the child component needs re-rendering? 如何在React Native中阻止子组件重新呈现父组件? - How to stop a child component from re-rendering a parent component in react native? React子组件不会在父状态更改时重新呈现 - React child component not re-rendering on parent state change 如果父级更新反应js中的上下文或状态,如何停止重新渲染子组件? - how to stop re-rendering of child component if parent update the context or state in react js? 如何在不重新渲染组件的情况下更改 useState 的状态,而只是重新渲染其组件 - How to change useState's state without re-rendering the component, but just its components 停止重新渲染父组件。 仅重新渲染子组件 - Stop Re-Rendering of Parent Components. Only Re-Render Child Component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM