My code is this:
handleFavourite = id => {
const { restaurants } = this.state;
let newRestaurants = [...restaurants];
newRestaurants = sort(newRestaurants, 'favourite');
console.log(newRestaurants); // ALL GOOD
this.setState({ restaurants: newRestaurants }, () => console.log(this.state.restaurants)); // CONSOLE LOG POSTS OLD DATA
};
So there. The callback function in setState
shows old data and in UI nothing gets sorted as well. Why is it that way?
edit: Found the problem. I'm also using static getDerivedStateFromProps
and it's resetting the state every time.