我有组件连接到 redux 存储,它从 props 获取数据:
const mapStateToProps = state => ({rowData: dataSelector(state)})
组件有自己的状态:
this.state = {
rowsPerPage: 23,
pageCount: 0,
}
当props.rowData
发生变化时,我需要计算新的state.pageCount
。我该怎么做?
您可以使用getSnapshotBeforeUpdate
通过使用 if 条件来确定props.rowData
何时更改。根据更改值的时间,您可以根据需要更新state.pageCount
。
請 create componentWillReceiveProps
componentWillReceiveProps(newProps) {
if(newProps.rowData.length !== this.state.pageCount) {this.setState({pageCount: newProps.rowData.length})}
}