伙计们,我是React的新手,在一次调用中两次更新State时遇到问题。
我正在尝试根据其性别在反应表上过滤可见的行。
下面是我的州,其中已过滤并更新了数据,还有一个备份 defaultData 以重置数据。
this.state = {
data: [
{'id':1,first_name:'Ilka',last_name:'Riddel',email:'iriddel0@blogger.com',gender:'Female'},
{'id':2,first_name:'Myrwyn',last_name:'Skinner',email:'mskinner1@cmu.edu',gender:'Male'},
{'id':3,first_name:'Almira',last_name:'Kubu',email:'akubu2@google.ru',gender:'Female'},
{'id':4,first_name:'Eloisa',last_name:'Maltster',email:'emaltster3@phoca.cz',gender:'Female'},
{'id':5,first_name:'Vite',last_name:'Capin',email:'vcapin4@noaa.gov',gender:'Male'},
{'id':6,first_name:'Zelig',last_name:'Crippin',email:'zcrippin5@blogtalkradio.com',gender:'Male'},
{'id':7,first_name:'Persis',last_name:'Middlemiss',email:'pmiddlemiss6@discovery.com',gender:'Female'},
{'id':8,first_name:'Amelina',last_name:'Biddy',email:'abiddy7@noaa.gov',gender:'Female'},
{'id':9,first_name:'Darrell',last_name:'Kemme',email:'dkemme8@clickbank.net',gender:'Male'},
{'id':10,first_name:'Kerianne',last_name:'Formby',email:'kformby9@berkeley.edu',gender:'Female'}
],
defaultData: [
{'id':1,first_name:'Ilka',last_name:'Riddel',email:'iriddel0@blogger.com',gender:'Female'},
{'id':2,first_name:'Myrwyn',last_name:'Skinner',email:'mskinner1@cmu.edu',gender:'Male'},
{'id':3,first_name:'Almira',last_name:'Kubu',email:'akubu2@google.ru',gender:'Female'},
{'id':4,first_name:'Eloisa',last_name:'Maltster',email:'emaltster3@phoca.cz',gender:'Female'},
{'id':5,first_name:'Vite',last_name:'Capin',email:'vcapin4@noaa.gov',gender:'Male'},
{'id':6,first_name:'Zelig',last_name:'Crippin',email:'zcrippin5@blogtalkradio.com',gender:'Male'},
{'id':7,first_name:'Persis',last_name:'Middlemiss',email:'pmiddlemiss6@discovery.com',gender:'Female'},
{'id':8,first_name:'Amelina',last_name:'Biddy',email:'abiddy7@noaa.gov',gender:'Female'},
{'id':9,first_name:'Darrell',last_name:'Kemme',email:'dkemme8@clickbank.net',gender:'Male'},
{'id':10,first_name:'Kerianne',last_name:'Formby',email:'kformby9@berkeley.edu',gender:'Female'}
]
};
我有处理程序方法来过滤数据并更新状态。
如果他们单击“显示所有男人”,那么handleShowOnlyMale功能将过滤状态下的数据并删除数据中的所有女性,然后单击< strong> showAllFemale 表格将为空,因为所有女性均已删除。我不希望这种行为,并且希望在表中的男性和女性记录之间产生类似切换的效果。
handleResetData(){
//console.log(this.state.data);
this.setState({
data: this.state.defaultData
})
}
handleShowOnlyMale(){
//handleResetData();
// this.setState((prevState) => {
// return {data: prevState.defaultData};
// }),
this.setState({
data: this.state.data.filter(person => person.gender === 'Male')
})
}
handleShowOnlyFemale(){
//handleResetData();
// this.setState((prevState) => {
// return {data: prevState.defaultData};
// }),
this.setState({
data: this.state.data.filter(person => person.gender === 'Female')
})
}
我的解决方法是每次开始过滤之前更新数据:this.state.defaultData。
如何正确执行此操作?
以下是我的完整代码:
import React from "react";
import { render } from "react-dom";
// Import React Table
import ReactTable from "react-table";
import "react-table/react-table.css";
class App extends React.Component {
constructor() {
super();
this.state = {
data: [
{'id':1,first_name:'Ilka',last_name:'Riddel',email:'iriddel0@blogger.com',gender:'Female'},
{'id':2,first_name:'Myrwyn',last_name:'Skinner',email:'mskinner1@cmu.edu',gender:'Male'},
{'id':3,first_name:'Almira',last_name:'Kubu',email:'akubu2@google.ru',gender:'Female'},
{'id':4,first_name:'Eloisa',last_name:'Maltster',email:'emaltster3@phoca.cz',gender:'Female'},
{'id':5,first_name:'Vite',last_name:'Capin',email:'vcapin4@noaa.gov',gender:'Male'},
{'id':6,first_name:'Zelig',last_name:'Crippin',email:'zcrippin5@blogtalkradio.com',gender:'Male'},
{'id':7,first_name:'Persis',last_name:'Middlemiss',email:'pmiddlemiss6@discovery.com',gender:'Female'},
{'id':8,first_name:'Amelina',last_name:'Biddy',email:'abiddy7@noaa.gov',gender:'Female'},
{'id':9,first_name:'Darrell',last_name:'Kemme',email:'dkemme8@clickbank.net',gender:'Male'},
{'id':10,first_name:'Kerianne',last_name:'Formby',email:'kformby9@berkeley.edu',gender:'Female'}
],
defaultData: [
{'id':1,first_name:'Ilka',last_name:'Riddel',email:'iriddel0@blogger.com',gender:'Female'},
{'id':2,first_name:'Myrwyn',last_name:'Skinner',email:'mskinner1@cmu.edu',gender:'Male'},
{'id':3,first_name:'Almira',last_name:'Kubu',email:'akubu2@google.ru',gender:'Female'},
{'id':4,first_name:'Eloisa',last_name:'Maltster',email:'emaltster3@phoca.cz',gender:'Female'},
{'id':5,first_name:'Vite',last_name:'Capin',email:'vcapin4@noaa.gov',gender:'Male'},
{'id':6,first_name:'Zelig',last_name:'Crippin',email:'zcrippin5@blogtalkradio.com',gender:'Male'},
{'id':7,first_name:'Persis',last_name:'Middlemiss',email:'pmiddlemiss6@discovery.com',gender:'Female'},
{'id':8,first_name:'Amelina',last_name:'Biddy',email:'abiddy7@noaa.gov',gender:'Female'},
{'id':9,first_name:'Darrell',last_name:'Kemme',email:'dkemme8@clickbank.net',gender:'Male'},
{'id':10,first_name:'Kerianne',last_name:'Formby',email:'kformby9@berkeley.edu',gender:'Female'}
],
firstNameSelected: true,
showMale: true,
showFemale: true
};
}
handleInputChange(value) {
console.log(this.state.data);
this.setState({ firstNameSelected: !this.state.firstNameSelected });
}
handleToUpperCaseFirstName(){
// this.setState({
// data: this.state.data.map(a => a.toUpperCase())
// })
}
handleResetData(){
//console.log(this.state.data);
this.setState({
data: this.state.defaultData
})
}
handleShowOnlyMale(){
//handleResetData();
// this.setState((prevState) => {
// return {data: prevState.defaultData};
// }),
this.setState({
data: this.state.data.filter(person => person.gender === 'Male')
})
}
handleShowOnlyFemale(){
//handleResetData();
// this.setState((prevState) => {
// return {data: prevState.defaultData};
// }),
this.setState({
data: this.state.data.filter(person => person.gender === 'Female')
})
}
render() {
const { data, firstNameSelected } = this.state;
return (
<div>
<div>
<button
onClick={this.handleToUpperCaseFirstName}>
HIGHLIGHT FIRSTNAME (UPPERCASE)
</button>
<button
onClick={this.handleShowOnlyMale.bind(this)}>Show All Men</button>
<button
onClick={this.handleShowOnlyFemale.bind(this)}>Show All The Ladies</button>
</div>
<ReactTable
data={data}
columns={[
{
columns: [
{
Header: "First Name",
accessor: "first_name",
show: firstNameSelected
},
{
Header: "Last Name",
id: "last_name",
//show: firstNameSelected,
accessor: d => d.last_name
}
]
},
{
columns: [
{
Header: "Email",
accessor: "email"
},
{
Header: "Gender",
accessor: "gender"
}
]
},
]}
defaultPageSize={10}
className="-striped -highlight"
/>
<br />
</div>
);
}
}
render(<App />, document.getElementById("root"));
答案 0 :(得分:1)
您可以保持数据状态并仅返回经过过滤的值,如下所示:
handleShowOnlyMale() {
return this.state.data.filter(person => person.gender === 'Male');
}
handleShowOnlyFemale(){
return this.state.data.filter(person => person.gender === 'Female');
}
答案 1 :(得分:0)
为什么不使用defaultData进行过滤
handleShowOnlyMale(){
this.setState({
data: this.state.defaultData.filter(person => person.gender === 'Male')
})
}
handleShowOnlyFemale(){
this.setState({
data: this.state.defaultData.filter(person => person.gender === 'Female')
})
}