In the ReactJS, I am changing the route to some new one with an "id", then based on this "id", I must call an API and fetch data.
I have used the API call in componentDidMount and componentWillMount and tried setState() to have my data in the state. but they didn't work in my case.
The problem is in the render() part when I want to use my data (from state), the data is not there because the API call takes some time to update the state.
Here is my code:
componentDidMount() {
api.get(id).then((response) => {
this.setState({response,});
});
With this approach, I don't have the data when I want it (render method), it will eventually be in the state but too late!
How can I change my approach to fix the loading problem?