2017-04-18 49 views
1

这是一个简单的演示。shouldComponentUpdate在react-native中不起作用

//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发生变化时,我想阻止Filter rerender

+0

您可以使用应用程序状态以及componentWillReceiveProps来控制组件可见性。 –

+0

勾选 - > https://snack.expo.io/By2E2IX0x –

回答

0

shouldComponentUpdate只在props/state变化时被调用。由于您的代码不会更改Filter组件中或上方的状态/道具,因此无需调用shouldComponentUpdate(不需要初始安装渲染)。

相关问题