【发布时间】:2017-04-03 06:21:58
【问题描述】:
我在 react 中遇到了一个与更新状态有关的问题。
嗯,我有一些数据,我想用这些数据构建一个表。但是,我想先过滤它。过滤效果很好,但我只有更新过滤数据并将其扔给下一个组件的问题......(我知道 setState 没有立即工作......)
ReportTable 组件中的updatedReports 仍然没有过滤数据... 修复它并使用更新数组状态的最佳方法是什么。
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 选项参数很好......当我记录 updatedReports - 我得到了我想要的......问题是 currentReports 没有立即更新。