React Native Child Component Not Updated when redux state changes

一笑奈何 提交于 2019-12-08 12:54:56

问题


I have child component that renders a list of items in a ListView. The data for the ListView originates from a Redux store object passed through the parent mapStateToProps. The child object has buttons that will remove an item when pressed. The button fires the appropriate redux dispatch actions and the status is updated correctly, but the child component does not update. Through break points and console statements, I've verified that child componentShouldUpdate and child componentWillReceiveProps get fired when the redux state chagnes, but the child render method does not fire after the state change.

Parent

 <PendingActionsList
        proposedStatusChanges={this.props.proposedStatusChanges}
        certifications={this.props.certifications}
        driverProfile={this.props.driverProfile}
        acceptProposedStatusChange={this.props.acceptProposedStatusChange}
        rejectProposedStatusChange={this.props.rejectProposedStatusChange}
        navigator={this.props.navigator}
      />

Child

    componentWillMount(){
    this.setState({
      proposedChanges:this.props.proposedStatusChanges
    });

  }
  shouldComponentUpdate(nextProps){
//fires when expected and returns expected value
    return nextProps.proposedStatusChanges.length != nextProps.proposedStatusChanges.length;
  }
  componentWillReceiveProps(nextProps){
    //fires when props change as expected
    console.log({'will receive props': nextProps});
    this.setState({
      proposedChanges:nextProps.proposedStatusChanges
    });
  }

 render(){
//only fires at intial render
const dataSource = this.ds.cloneWithRows(this.state.proposedChanges)
    return(
      <View style={styles.container}>
       <Text>Pending Actions </Text>
        <ListView
          dataSource={dataSource}
          renderRow={(rowData) => this.renderRow(rowData)}
          renderSeparator={(sectionId,rowId)=><View key={`${sectionId}-${rowId}`} style={{height:1,backgroundColor:'#D1D1D1'}}/>}
          />
      </View>
    );

I've tried this without the state field too, which is what I was expecting to work, but had the same results.


回答1:


That's because you're checking for inequality in the same object all the time:

//Replace this:
return nextProps.proposedStatusChanges.length != nextProps.proposedStatusChanges.length;
//With this:
return nextProps.proposedStatusChanges.length != this.props.proposedStatusChanges.length;


来源:https://stackoverflow.com/questions/38470500/react-native-child-component-not-updated-when-redux-state-changes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!