オブジェクトの配列からレポートのリストを作成する単純なコンポーネントがあります。フィルターと並べ替え機能を追加しようとしています (これはある程度機能します)。並べ替え機能は機能しますが、変異しているのではないかと心配しています。元の状態を新しい配列にコピーしようとしたにもかかわらず、状態。
私のフィルターは初めて機能しますが、状態が変更されているため、または初期状態をフィルター処理できないため、他の結果はフィルター処理されませんか? これは私を何時間も混乱させてきました。どんな助けも大歓迎です。
どうもありがとう
constructor(props) {
super(props);
this.state = {
reports: props.data
};
this.handleSortBy = this.handleSortBy.bind(this);
this.handleFilterType = this.handleFilterType.bind(this);
}
handleSortBy(event) {
const copy = [...this.state.reports];
if (event.target.value === 'A-Z') {
return this.setState({
reports: copy.sort((a, b) => a.name.localeCompare(b.name))
});
}
if (event.target.value === 'Z-A') {
this.setState({
reports: copy
.sort((a, b) => a.name.localeCompare(b.name))
.reverse()
});
}
}
handleFilterType(event) {
this.setState({
reports: this.state.reports.filter(item => {
return item.type === event.target.value;
})
});
}
前もって感謝します :)