我正在使用'onChange'方法对数组进行排序,并使用'setState'对其进行设置,但之后将其变为未排序;
我有更新功能
// this method is called as a callback from component (on component change)
async onSomeComponentUpdate(data) {
let newData = [];
// ... some sorting fctions
console.log(newData); // it's sorted here
this.setState(state=> ({data: newData}));
}
之后,我记录了渲染功能
render() {
// data are still sorted here
Object.keys(this.state.data).map((key) => {
console.log(this.state.data[key]);
});
// but down here it is not sorted
return (
{Object.keys(this.state.data).map((key) => {
return (this.SomeComponentRow(key,this.state.data[key]))
})}
);
}
和绑定了onUpdate的组件函数
SomeComponentRow(key, item) {
return <SomeComponent key={key} item={item} onUpdate={this.onSomeComponentUpdate} />;
}