简体   繁体   English

React:如何使用 innerHTML 卸载子节点?

[英]React: How to use innerHTML to unmount child nodes?

I have say around 30K+ components on the page.我在页面上说过大约 30K+ 组件。 On click of a button I unmount all the 30K+ components via conditional rendering which becomes super slow around 10+ seconds and causes the page to hang.单击按钮后,我通过条件渲染卸载所有 30K+ 组件,这在 10+ 秒左右变得超级慢并导致页面挂起。 On the other hand setting the innerHTML to "" on the parent container is able to do it in milliseconds.另一方面,在父容器上将 innerHTML 设置为 "" 可以在几毫秒内完成。 But the problem with the innerHTML approach is React is not able to know whether these components were removed from the DOM or not, so on the next render all the 30K+ components reappear.但是 innerHTML 方法的问题是 React 无法知道这些组件是否已从 DOM 中删除,因此在下一次渲染时,所有 30K+ 组件都会重新出现。 Is there some way to use innerHTML to remove all the nodes but still ensure React knows that all the components have been unmounted?有什么方法可以使用 innerHTML 删除所有节点,但仍确保 React 知道所有组件都已卸载?

I was able to use the clearLayers method of Leaflet.js to clear all the marker nodes.我能够使用 Leaflet.js 的 clearLayers 方法清除所有标记节点。 This fixed the unmount slowness.这修复了卸载缓慢。

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

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