Upgrade to Pro — share decks privately, control downloads, hide ads and more …

最近 React Native を触り始めて 〜ListView ハマりポイント〜

diskshima
November 04, 2016

最近 React Native を触り始めて 〜ListView ハマりポイント〜

2016/11/04 の「React&React Native入門者の会」のLTのプレゼン資料です。
React Native の ListView でハマるポイントを1つ紹介しています。

diskshima

November 04, 2016
Tweet

Other Decks in Programming

Transcript

  1. ListView あるある 簡単なサンプルアプリを作りました。 this.data = [ new Player('Eric Cantona'), new

    Player('David Beckham'), new Player('Ryan Giggs'), new Player('Gary Neville'), ]; const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2}); this.state = { dataSource: ds.cloneWithRows(this.data) }; データを読み込む部分はこんな感じです。
  2. ListView あるある 「Add Ronaldo」 ボタンを押した時のコード onButtonPress() { const newPlayer =

    new Player('Cristiano Ronaldo'); this.data.push(newPlayer); } this.data は ListViewDataSource に追加済みだから、動く でしょ。
  3. ドキュメントを読む To update the data in the datasource, use cloneWithRows

    (or cloneWithRowsAndSections if you care about sections). The data in the data source is immutable, so you can't modify it directly. The clone methods suck in the new data and compute a diff for each row so ListView knows whether to re-render it or not.
  4. ドキュメントを読む To update the data in the datasource, use cloneWithRows

    (or cloneWithRowsAndSections if you care about sections). The data in the data source is immutable, so you can't modify it directly. The clone methods suck in the new data and compute a diff for each row so ListView knows whether to re-render it or not. datasource を更新するときは cloneWithRows か cloneWithRowsAndSections を呼んでね。 datasource の中にあるデータは変えられないけど、この clone メソッドでデータを読み取って、差分を計算の 上、再描画するかどうか決めます。 訳した
  5. とりあえず呼んでみる ボタンハンドラを変えて呼んでみる。 onButtonPress() { const newPlayer = new Player('Christiano Ronaldo');

    this.data.push(newPlayer); // ↓↓↓ 追加 ↓↓↓ this.setState({ dataSource: this.state.dataSource.cloneWithRows(this.data) }); }
  6. ソースの旅(長いから詳細は割愛) • dirty って名前があったのは _calculateDirtyArray(dirty チェック) ◦ dirty = 変更があったかのチェック

    ◦ rowHasChanged もこのメソッドのみ ▪ ListViewDataSource.js#L383 らへん • _calculateDirtyArray を呼んでいるのは cloneWithRowsAndSections だけ • cloneWithRows は cloneWithRowsAndSections の薄いラッパー