React Native Child 当 redux 状态改变时组件未更新
React Native Child Component Not Updated when redux state changes
我有 child 组件,它在 ListView 中呈现项目列表。 ListView 的数据源自通过 parent mapStateToProps 传递的 Redux 存储 object。 child object 有一些按钮,按下时会删除一个项目。该按钮会触发适当的 redux 调度操作并且状态会正确更新,但 child 组件不会更新。通过断点和控制台语句,我已经验证 child componentShouldUpdate 和 child componentWillReceiveProps 在 redux 状态变化时被触发,但是 child render 方法在状态改变后不会触发。
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>
);
我也在没有状态字段的情况下尝试过这个,这是我期望的工作,但结果相同。
那是因为您一直在检查同一对象中的不等式:
//Replace this:
return nextProps.proposedStatusChanges.length != nextProps.proposedStatusChanges.length;
//With this:
return nextProps.proposedStatusChanges.length != this.props.proposedStatusChanges.length;
我有 child 组件,它在 ListView 中呈现项目列表。 ListView 的数据源自通过 parent mapStateToProps 传递的 Redux 存储 object。 child object 有一些按钮,按下时会删除一个项目。该按钮会触发适当的 redux 调度操作并且状态会正确更新,但 child 组件不会更新。通过断点和控制台语句,我已经验证 child componentShouldUpdate 和 child componentWillReceiveProps 在 redux 状态变化时被触发,但是 child render 方法在状态改变后不会触发。
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>
);
我也在没有状态字段的情况下尝试过这个,这是我期望的工作,但结果相同。
那是因为您一直在检查同一对象中的不等式:
//Replace this:
return nextProps.proposedStatusChanges.length != nextProps.proposedStatusChanges.length;
//With this:
return nextProps.proposedStatusChanges.length != this.props.proposedStatusChanges.length;