【问题标题】:Does it make sense to compare arrays of objects in React's shouldComponentUpdate?在 React shouldComponentUpdate 中比较对象数组是否有意义?
【发布时间】:2016-10-04 12:48:33
【问题描述】:

我正在尝试使用 React,我正在尝试优化组件更新等。

我正在构建一个返回对象数组的搜索组件。为了优化它,如果执行新搜索后搜索结果相同,我试图阻止我的组件更新。

现在我正在使用 Immutable 来比较它们并且它有效。

关于这方面的性能方面的任何 cmet?一般来说这样做有意义吗?还有更有效的方法吗?

这是我的组件。

class ItemsListContainer extends React.Component {

  componentDidMount() {
    itemsApi.getItems();
    store.dispatch(loadSearchLayout('items', 'found items'));
  }

  shouldComponentUpdate(nextProps, nextState) {
    let prevItems = Immutable.fromJS(this.props.items);
    let nextItems = Immutable.fromJS(nextProps.items);
    return !Immutable.is(prevItems, nextItems)
  }

  render() {
    return (
      <ItemsList items={this.props.items} />
    );
  }

};

const mapStateToProps = function(store) {
  return {
    items: store.itemsState.items
  };
};

export default connect(mapStateToProps)(ItemsListContainer);

【问题讨论】:

    标签: javascript arrays performance reactjs javascript-objects


    【解决方案1】:

    如果我有必要的要点,我会将此作为评论。

    Performance issues with a tree structure and shouldComponentUpdate in React / Redux

    【讨论】:

    • 嘿,虽然这非常有用,但我不知道。这不是我要问的情况,因为在我的情况下,我不是在更新树内的对象,而是更新整个树。我刚刚找到了一种使用不可变的方法。更新我的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-29
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 2022-01-09
    相关资源
    最近更新 更多