3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

React パフォーマンス改善 (Array系)

Last updated at Posted at 2019-06-27

改善対象

配列を繰り返し処理によって表示しており、ソートや絞り込み等が行われる場合、レンダリングコストや繰り返し表示する数によっては、何も意識せずに実装した時に著しくパフォーマンスが悪くなることがあります。

そういった時に、パフォーマンス低下を防ぐ簡単な方法をひとつご紹介します。

Step1: Componentを改善する

React.PureComponentshouldComponentUpdate などを用いて再レンダリングを制御する。
shallowEqualの判定を考慮して使い分けが必要です。


items = [{value: 1}, {value: 2}, {value: 3}]

{items.map(item => (
  <Item obj={item} />
))}

React.PureComponent


Class Item extends React.PureComponent {
  render() {
    return <div>{this.props.obj.value}</div>
  }
}

shouldComponentUpdate


Class Item extends React.Component {
  shouldComponentUpdate(nextProps) {
    return this.props.obj.value !== nextProps.obj.value
  }

  render() {
    return <div>{this.props.obj.value}</div>
  }
}

改善例

表示していたListが下記のように並び順が変わった場合

items = [
  {value: 1}, // key: 1 
  {value: 2}, // key: 2  
  {value: 3}  // key: 3 
]

items = [
  {value: 3}, // key: 1 
  {value: 2}, // key: 2  
  {value: 1}  // key: 3 
]

{value: 2} はkeyが変わらずかつ shouldComponentUpdate で比較されているobj.valueの値も同じため、
{value: 2} のComponent(<Item />)は再レンダリングされなくなります。

※ この例では、keyを指定していないためデファルトでindexが使われています。

Step2: ユニークかつ適切なkeyを指定する

shouldComponentUpdate と key を 利用

※ PureComponentの例は省略します。

Class Item extends React.Component {
  shouldComponentUpdate(nextProps) {
    return this.props.obj.value !== nextProps.obj.value
  }

  render() {
    return <div>{this.props.obj.value}</div>
  }
}

{items.map(item => (
  <Item key={item.obj.value} obj={item.obj} />
))}

改善例

表示していたListが下記のように並び順が変わった場合

items = [
  {value: 1}, // key: 1 
  {value: 2}, // key: 2  
  {value: 3}  // key: 3 
]

items = [
  {value: 3}, // key: 3 
  {value: 2}, // key: 2  
  {value: 1}  // key: 1 
]

この例では、keyが変わらずかつobj.valueの値も同じため、
すべてのComponent(<Item />)が再レンダリングされなくなります。

DEMOページ

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?