开发者问题收集

在 React 中按对象键对对象的状态数组进行排序

2021-02-11
1569

我尝试根据状态中定义的一个键值对对表数据进行排序。

这是我的状态:

class Employee extends Component {
state = {
  employeeData: [],
  filteredEmployeeData: [],
  search: '',
  sort: 'asc',
  error: '',
}

这是我尝试用于排序的函数。这对我来说不起作用,我一直收到错误“未捕获的类型错误:无法读取未定义的属性‘state’”

sortFirstName(){
    console.log('SORTING!!!!')
    const {employeeData} = this.state
    const sorted = employeeData.sort((a, b) => {
          if (a.name.first < b.name.first){
            return -1
        } else if (a.name.first > b.name.first){
            return 1
        } else {
          return 0
        }
    })
    console.log('Sorted List: ', sorted)
    this.setState({filteredEmployeeData: sorted})
}

这是它的填充方式

render () {
    return (
      <div>
        <Container style={{ minHeight: "80%" }}>
        <br/>
          <h1 className="text-center">Search For An Employee</h1>
          <Alert
            type="danger"
            style={{ opacity: this.state.error ? 1 : 0, marginBottom: 10 }}
          >
            {this.state.error}
          </Alert>
          <SearchForm
            handleInputChange={this.handleInputChange}
            search={this.state.search}
          />
        </Container>
        <Container>
        <table className="table table-hover">
        <thead>
            <tr>
            <th scope="col" >Image</th>
            <th scope="col" value="firstName" onClick={(this.sortFirstName)}>First Name</th>
            <th scope="col" value="lastName">Last Lame</th>
            <th scope="col" value="email">Email</th>
            <th scope="col" value="phoneNumber">Phone Number</th>
            <th scope="col" value="city">City</th>
            <th scope="col" value="SSN">SSN</th>
            </tr>
        </thead>
        <tbody>
        {this.state.filteredEmployeeData.map(ee => (
          <EmployeeData
            id={ee.login.uuid}
            key={ee.login.uuid}
            img={ee.picture.thumbnail}
            firstName={ee.name.first}
            lastName={ee.name.last}
            email={ee.email}
            phone={ee.cell}
            city={ee.location.city}
            ssn={ee.id.value}
          />
        ))}
        </tbody>
        </table>
        </Container>
      </div>
    );
  }}

另外,我有一个 API 调用,在挂载时提取员工数据,并将 employeeData 和filteredEmployeeData 都设置为对象数组。我认为您不需要查看该部分即可获得它,但如果有帮助,我也可以分享它。

任何帮助都将不胜感激。

2个回答

sortFirstName 定义为箭头函数,它将 this 正确绑定到您的 Employee 类:

sortFirstName = () => {
    console.log('SORTING!!!!')
    const {employeeData} = this.state
    const sorted = employeeData.sort((a, b) => {
          if (a.name.first < b.name.first){
            return -1
        } else if (a.name.first > b.name.first){
            return 1
        } else {
          return 0
        }
    })
    console.log('Sorted List: ', sorted)
    this.setState({filteredEmployeeData: sorted})
}
buzatto
2021-02-11

有两种方法可以修复此错误。

  1. 在构造函数中绑定 this

    constructor(props) {
    super(props);
    this.state = {
    employeeData: [],
    filteredEmployeeData: [],
    search: '',
    sort: 'asc',
    error: ''
    }
    this.sortFirstName = this.sortFirstName.bind(this);
    }
    

或 2. 使用箭头函数

sortFirstName = () => {
Praveen Nambiar
2021-02-11