… Along with componentDidMount, You also need to implement the componentWillReceiveProps or use getDerivedStateFromProps(from v16.3.0 onwards) in Products page since the same component is re-rendered with updated params and not re-mounted when you change the route params, this is because params are passed as props to the component and on props change, React components re-render and not re-mounted. …
componentWillReceiveProps(newProps) { let id = newProps.match.params.id; if (typeof(id) !== 'undefined' && id !== null && id !== this.props.match.params.id) { console.log('will fetch new lesson...') this.fetchLesson(id); } }
componentDidMount修改如下:
1 2 3 4
componentDidMount() { let id = this.props.match.params.id; this.fetchLesson(id); }