reactjsag-gridreact-lifecycle-hooks

How to update props dependent state


I have component connected to redux store which gets data from props:

const mapStateToProps = state => ({rowData: dataSelector(state)})

The component has its own state:

this.state = {
  rowsPerPage: 23,
  pageCount: 0,
}

I need to calculate new state.pageCount when props.rowData changes. How can i do it?


Solution

  • You can use getSnapshotBeforeUpdate to determine when the props.rowData changes by using an if condition. Based on when the value is changed, you can update your state.pageCount like the way you want.