I am trying to update state whenever the component gets updated. I do so because I reuse the component to show different data from the API. But there is a problem - componentDidUpdate creates an infinite loop even though I check if the state is different from the response data.
I will add some code.
componentDidUpdate() {
const { products } = this.state;
axios
.get(`http://localhost/wp-json/wp/v2/posts?categories=${this.props.match.params.id}`)
.then(res => {
if (res.data !== products) {
this.setState({ products: res.data, loading: false });
} else {
console.log('matches');
}
})
.catch(err => console.log(err));
}
Is there any work-around to avoid making an API request every time the component gets updated? I have an API request in my componentDidMount function, but it works only for the initial render.