反応で状態を更新することに関連する1つの問題に遭遇しました。
さて、いくつかのデータがあり、このデータを使用してテーブルを作成したいと考えています。しかし、最初にフィルタリングしたいと思います。フィルタリングはうまく機能していますが、フィルタリングされたデータの更新と次のコンポーネントへのスローに問題があるだけです...(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>
);
}
}