我的代码是这样的:
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
};
所以在那里。回调函数setState显示旧数据,在 UI 中也没有任何东西被排序。为什么会这样?
编辑:发现问题。我也在使用static getDerivedStateFromProps,它每次都重置状态。
婷婷同学_
慕运维8079593
慕容3067478
相关分类