snippet
Fetch data when React component props changes
Here is an example to refresh the content of a component only when you have a change of props. If you have a changing props, you will force the component to be refreshed. In the case where no props are changed the component will refresh without updating the data.
class MyComponent extends Component {
componentDidMount() {
this.fetchData();
}
componentDidUpdate(prevProps) {
if (prevProps.params.id !== this.props.params.id) {
this.fetchData();
}
}
fetchData() {
this.props.fetchData(this.props.params.id);
}
}
Related snippets |
---|