简体   繁体   English

React 为什么卸载组件很慢

[英]React Why Unmount Component Slow

I have a component that wraps a bunch of ReactList components.我有一个包含一堆 ReactList 组件的组件。 ReactList component is a component that does infinite scrolling ie only load what's on viewport. ReactList 组件是一个可以无限滚动的组件,即只加载视口上的内容。 It has couple of modes: simple and uniform.它有两种模式:简单和统一。 I'm using simple which just loads and loads as you scroll down (making the scrollable page longer).我正在使用 simple ,它只是在您向下滚动时加载和加载(使可滚动页面更长)。 Uniform loads and removes the above section that is now out of viewport. Uniform 加载并移除现在超出视口的上述部分。 I tried using uniform mode, and if it worked I wouldn't even need to ask what I'm about to, but it's super buggy and defeats the purpose.我尝试使用统一模式,如果它有效,我什至不需要问我要做什么,但它是超级越野车并且违背了目的。 Simple on the other hand is super fast scrolling and loading is equally fast.简单的另一方面是超快速滚动和加载同样快。

I broken down my list into few groups where user can click and it will load that ReactList component.我将我的列表分成几组,用户可以在其中单击并加载该 ReactList 组件。 If the scrollable page is short, ie, user hasn't scrolled all the way down, changing between the group of lists is fast enough.如果可滚动页面很短,即用户没有一直向下滚动,则在列表组之间切换就足够快了。 ~2s. 〜2秒。 However, if the page is scrolled all the way down, and trying to change list takes ~6s.但是,如果页面一直向下滚动,并且尝试更改列表需要大约 6 秒。

I noticed that the page is fully loaded too, ie nothing more to load.我注意到页面也已完全加载,即没有更多内容可加载。 So okay, I think maybe if I change the key for the wrapper parent component and just remount the component, it should be fast right?好吧,我想也许如果我更改包装器父组件的键并重新安装组件,它应该很快吧? Nope.不。 The component that reloads is short, only one viewport length, but it takes around ~5s.重新加载的组件很短,只有一个视口长度,但大约需要 5 秒。

I noticed changing out of the fully loaded list component page to just regular single line text "Hello world" component still took around the same amount!我注意到从完全加载的列表组件页面更改为普通的单行文本“Hello world”组件仍然占用相同的数量! ~5s. 〜5秒。 What's up with that?那是怎么回事?

QUESTION: Why is unmounting a long page with many small components in a list take so long to just unmount?问题:为什么卸载一个列表中包含许多小组件的长页面需要很长时间才能卸载?

Sorry I realize this is long and full of text but I thought I should explain my situation.抱歉,我意识到这篇文章又长又全,但我想我应该解释一下我的情况。

As it is an old question, I really want to put it to an end.由于这是一个老问题,我真的很想结束它。

Quick answer: use virtual list, something like uniform mode you described, but better maintained with better api.快速回答:使用虚拟列表,类似于您描述的uniform mode ,但使用更好的 api 可以更好地维护。 You can get bunch of packages if you search react virtual list , use ones with more stars and better maintained, and most of all fit your case.如果您搜索react virtual list ,您可以获得大量软件包,使用具有更多星级和更好维护的软件包,并且最适合您的情况。

Surely you got lots of small components when you scroll.当你滚动时,你肯定会得到很多小组件。 In react it's not like removing a node is one shot thing, it needs to unmount every leaf from the tree, and recursively unmount leaves untuil reach that node.在反应中,它不像删除节点是一次性的事情,它需要从树中卸载每个叶子,并递归卸载叶子直到到达该节点。 So if you got a lot of components, even today's new react won't help you out.所以如果你有很多组件,即使是今天的新 react 也帮不了你。

With virtual list, you get less components although your user doesn't know, so it would be quick.使用虚拟列表,虽然您的用户不知道,但您获得的组件较少​​,因此速度会很快。

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

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