I have a Profile component that shows user profile. On route change to user/:userId, the userId query param will be updated in ownProps thanks to react-router-redux and will then be passed into my presentation component:
const mapStateToProps = (state, ownProps) => ({
userId: ownProps.params.userId,
user: store.profile.user //null at first, will be updated later
})
At this point, I need to fetch user profile from userId. Where should I do this fetching? Currently I am doing it in componentDidMount
componentDidMount() {
this.props.fetchProfile(this.props.userId) //this will dispatch an action to fetch user details and store in `store.profile.user`
}
The downside of this is if user goes from /users/1 to /users/2, componentDidMount wont trigger. How should this be done?