排序数组后未渲染渲染

时间:2019-07-10 12:32:53

标签: reactjs

我正在使用'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} />;
}

1 个答案:

答案 0 :(得分:0)

Object.keys不保证顺序。如果订单很重要,则需要改用数组(您可以找到更多信息here