我遇到了一個與更新狀態有關的問題。如何立即爲數組設置狀態?
那麼,我有一些數據,我想用這些數據建立一個表。但是,我想先過濾它。過濾運行良好,但我只有更新過濾的數據並將其投擲到下一個組件...(我知道setState不能立即工作...)
已更新reportReable組件中的報表仍未過濾data ... 什麼是修復它並使用更新數組狀態的最佳方法。
export default class ReportContent extends React.Component {
constructor(props) {
super(props);
this.state = {
currentReports: this.props.reports
};
}
_filterBy(option) {
let updatedReports = [...this.props.reports].filter(report => {
if (report.organizations === option || report.reportType === option) {
return report;
}
});
console.log(updatedReports);
this.setState({currentReports: updatedReports});
}
render() {
return (
<div className="reports-table">
<ReportMenu organizations={this.props.organizations} reportTypes={this.props.reportTypes}
filterBy={this._filterBy.bind(this)}/>
<ReportTable updatedReports={this.state.currentReports}/>
</div>
);
}
}
我沒有看到任何錯誤的代碼,除了'_filterBy'方法中'option'參數的結構。你可以發佈你的'ReportMenu'組件和''option'參數的結構。 – jpdelatorre
@jpdelatorre選項參數很好......當我登錄updatedReports時 - 我得到了我想要的......問題是currentReports不會立即更新。 – alingo