2017-04-18 15 views
1

これは簡単なデモです。shouldComponentUpdateは反応ネイティブでは機能しません

//data demo 
{ 
    filterOption:{id:'xxx',name:'yyy'}, 
    list:[{id:'xxx',name:'yyy',msg:'zzz'},...] 
} 

// Component demo 
class Root extends React.Component { 
    shouldComponentUpdate(newProps){ 
     console.log(newProps) // work 
    } 
    render(){ 
     let { filterOption, list } = this.props; 
     return <View> 
       <Filter filterOption={filterOption} /> 
       <List list={list} /> 
      <View> 
    } 
} 
class Filter extends React.Component{ 
    shouldComponentUpdate(newProps){ 
     console.log(newProps) // not work 
    } 
    render(){ // <NativeFilter> is a native Component. 
     return <NativeFilter /> 
    } 
} 

質問:フィルターで

shouldComponentUpdateメソッドは動作しません。誰かが私を助けることができる? props.listが変更されたとき、フィルター再レンダリングを防止したいです。

+0

あなたはcomponentWillReceivePropsとともにアプリケーション状態を使用して、コンポーネントの可視性を制御できます。 –

+0

これをチェック - > https://snack.expo.io/By2E2IX0x –

答えて

0

shouldComponentUpdateは、小道具/状態の変更時にのみ呼び出されます。あなたのコードはFilterコンポーネント内の状態/小道具を変更しないので、shouldComponentUpdate(初期実装レンダリングのために呼び出されません)を呼び出す必要はありません。

関連する問題