gpt4 book ai didi

reactjs - 在React ES-6中设置State后未到达语句

转载 作者:行者123 更新时间:2023-12-03 12:36:40 24 4
gpt4 key购买 nike

在这个 react 代码中,我试图将数据设置为当前状态,将数据设置为状态后,我需要在固定数据表中使用它。要实现它,我写如下。主要问题是旁边的语句 componentDidMount 中的“this.setState ”未在我需要在那里调用数据表功能的地方执行/到达。
这是我的代码:

     export default class Main extends React.Component {
constructor(props) {
super(props);
this.state = {
result: [],
loading: false,
filteredRows: null,
filterBy: null,
};
}
getZipData() {
//Here i got the data from server
}
componentDidMount() {
this.getZipData().then((data)=>{
console.log("data:"+data.length);
this.setState({result:data,loading:true}); //state successfully set here.
console.log("result:*******************"+this.state.result.length); //this console is not printed
this._filterRowsBy(this.state.filterBy)
});

};
_rowGetter(rowIndex){
alert("row Getter");
return this.state.filteredRows[rowIndex];
}
_filterRowsBy(filterBy){
var rows = this.state.result.slice();
var filteredRows = filterBy ? rows.filter((row)=>{
return row.RecNo.toLowerCase().indexOf(filterBy.toLowerCase()) >= 0
}) : rows;
this.setState({
filterRows,
filterBy,
})
}
_onFilterChange(e){
this._filterRowsBy(e.target.value);
}
render() {
if(this.state.loading==false) {
return (
<div className="row-fluid">
<img className="col-sm-6 col-sm-offset-3 col-xs-6 col-xs-offset-3"
src="http://www.preciseleads.com/images/loading_animation.gif"/>
</div>
)
}
else{
console.log("result:////////////////////"+this.state.result.length); //this console is printed
console.log("loading completed");
return(
<div>
<!-- fixed data table code goes here -->
</div>
)
}

}
}

状态已成功更新,但未达到该语句之后,IN componentDidMount
任何帮助,不胜感激。

最佳答案

将捕获处理程序添加到您的Promise中。我想您遇到了错误。

关于reactjs - 在React ES-6中设置State后未到达语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32451009/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com