Дядя Фея
Дядя Фея

Reputation: 359

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?

Upvotes: 0

Views: 176

Answers (2)

Nikhil Goyal
Nikhil Goyal

Reputation: 1973

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.

Upvotes: 1

JLD
JLD

Reputation: 570

Please create componentWillReceiveProps

componentWillReceiveProps(newProps) {
if(newProps.rowData.length !== this.state.pageCount) {this.setState({pageCount: newProps.rowData.length})}
}

Upvotes: 0

Related Questions