React/Redux 中树结构和 shouldComponentUpdate 的性能问题 [英] Performance issues with a tree structure and shouldComponentUpdate in React / Redux

查看:18
本文介绍了React/Redux 中树结构和 shouldComponentUpdate 的性能问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 React、Redux 和 ImmutableJS 还很陌生,遇到了一些性能问题.

I'm fairly new to React, Redux and ImmutableJS, and have run into some performance issues.

我有一个大的数据树结构,我目前在这个结构中存储为一个平面列表:

I have a large tree structure of data, which I'm currently storing as a flat list in this structure:

new Map({
  1: new Node({
    id: 1,
    text: 'Root',
    children: [2,3]
  }),
  2: new Node({
    id: 2,
    text: 'Child 1',
    children: [4]
  }),
  3: new Node({
    id: 3,
    text: 'Child 2',
    children: []
  }),
  4: new Node({
    id: 4,
    text: 'Child of child 1',
    children: []
  })
});

虽然将其构建为平面列表使更新节点变得容易,但我发现随着树的增长,交互变得缓慢.交互包括能够选择一个或多个节点、切换其子节点的可见性、更新文本等.看起来 UI 缓慢的一个关键原因是每次交互都重新绘制整个树.

While structuring it as a flat list makes updating nodes easy, I'm finding that interaction gets sluggish as the tree grows. Interaction includes being able to select one or more nodes, toggle the visibility of their child nodes, update the text, and so on. It looks like a key reason for the the sluggish UI is that the entire tree is being redrawn for each interaction.

我想使用 shouldComponentUpdate 这样如果我更新节点 3,节点 2 和 4 就不会更新.如果将数据存储为树,这将很容易(我可以简单地检查是否 this.props !== nextProps),但是由于数据存储在一个平面列表中,检查会更多复杂.

I want to use shouldComponentUpdate such that if I update node 3, nodes 2 and 4 do not update. This would be easy if the data was stored as a tree (I could simply check whether this.props !== nextProps), but as the data is stored in a flat list the check would be substantially more complex.

我应该如何存储数据并使用 shouldComponentUpdate(或其他方法)来支持具有数百或数千个树节点的流畅 UI?

How should I being storing the data and using shouldComponentUpdate (or other methods) to support a smooth UI with hundreds or thousands of tree nodes?

编辑

我一直在顶级连接商店,然后不得不将整个商店传递给子组件.

I've been connecting the store at the top level, and then having to pass the entire store down to subcomponents.

我的结构是:

<NodeTree> 
  <NodeBranch>
    <Node>{{text}}</Node>
    <NodeTree>...</NodeTree>
  </NodeBranch>
  <NodeBranch>
    <Node>{{text}}</Node>
    <NodeTree>...</NodeTree>
  </NodeBranch>
  ...
</NodeTree>

可以用 shouldComponentUpdate 做一个简单的检查,看看标题是否改变了,但我没有类似的解决方案可以用在 给定树的递归性质,.

The <Node> can do a simple check with shouldComponentUpdate to see if the title has changed, but I have no similar solution to use on the <NodeTree> or <NodeBranch> given the recursive nature of the tree.

看起来最好的解决方案(感谢@Dan Abramov)是连接每个,而不是仅在顶层连接.我今晚将对此进行测试.

It looks like the best solution (thanks @Dan Abramov) would be to connect each <NodeBranch>, rather just connecting at the top level. I'll be testing this this evening.

推荐答案

刚刚添加一个新的例子就是这样.
你可以这样运行:

I just added a new example showing just that.
You can run it like this:

git clone https://github.com/rackt/redux.git

cd redux/examples/tree-view
npm install
npm start

open http://localhost:3000/

这篇关于React/Redux 中树结构和 shouldComponentUpdate 的性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆