I have a Node backend with React frontend. When a user creates a movie review, the post request gets handled with axios. Right after that, the user gets taken to a homepage but the review is not there yet. When I refresh the page, the review appears.
How can I fix this issue so that the user doesn't have to get confused when, after submitting, they don't see the review in homepage? Should I refetch every time a user hits the homepage? Or is React doing it behind the scenes but early so the review isn't registered?
My redux request code:
the reducer
export const postNewReview = review => (dispatch, getState) => {
return apiCall("post", `http://localhost:8000/review`, review)
.then(res => {})
.catch(err => addError(err.message));
};
the api call function
export function apiCall(method, path, data){
return new Promise((resolve,reject) => {
return axios[method.toLowerCase()](path,data)
.then(res => {
return resolve(res.data);
})
.catch(err => {
return reject(err.response.data.error);
});
});
}
handling submitting
handleSubmit = (e) => {
e.preventDefault();
this.props.postNewReview(this.state);
this.setState({user: '', title: '', image: '', text: ''});
this.props.history.push('/');
}