【问题标题】:Filtering object list with React使用 React 过滤对象列表
【发布时间】:2019-01-24 20:02:20
【问题描述】:

这可能是一个克隆帖子,但我没有找到任何解决方案。我有一个对象列表:

export default function() {
  return [
    {name: 'Mark Teer Stegen'},
    {name: 'Nelson Semedo'},
    {name: 'Gerrard Pique'},
    {name: 'Ivan Rakitic'},
    {name: 'Sergio Busquets'},
    {name: 'Denis Suarez'},
    {name: 'Coutinho'},
    {name: 'Luis Suarez'},
    {name: 'Lionel Messi'},
    {name: 'Dembele'},
    {name: 'Malcom'}
  ]
}

我将它导入到组件中,分配给状态并在下面的组件中显示。

import React, {Component} from 'react';
import {connect} from 'react-redux';

class Barca extends Component{
  constructor(props){
    super(props);

    this.state = {
      players: this.props.players,
      player: '' //empty to set as an input
    }
  }

  onChange(e){
    this.setState({
      player: e.target.value
    });
    console.log(this.state.player);
  }
  renderList(){
    return this.state.players.map((player) => {
      return(
        <tr key={player.name}>
          <td>{player.name}</td>
        </tr>
      );
    });
  }
  render(){
    return(
      <div className="col-sm-6 table-col table-responsive">
        <input
          type="text"
          value={this.state.player}
          onChange={this.onChange.bind(this)}
        />
        <table className="table table-striped">
          <thead>
            <tr>
              <th className="text-center">
                FC Barcelona
              </th>
            </tr>
          </thead>
          <tbody>
            {this.renderList()}
          </tbody>
        </table>
      </div>
    );
  }
}

const mapStateToProps = (state) => {
  return {
    players: state.reducerBarca
   };
};


export default connect(mapStateToProps)(Barca);

列表看起来像这样

问题是我想通过输入值过滤我的玩家列表。我在这里做了一些研究,我只在 Array 中找到了过滤,不像我在 Objects list 中的过滤。

我现在做了什么:

  1. 显示玩家列表
  2. 从输入中获取值并在每个书面字母后显示
  3. 如何按输入的术语呈现我的列表???

谢谢大家!我已经删除了玩家状态

constructor(props){
    super(props);

    this.state = {
      //players: this.props.players <-- Stupid thing
      player: '' //empty to set as an input
    }
  }

并重写我的renderList() 函数

return this.props.players.filter(player =>
        player.name.toLowerCase().includes(this.state.player.toLowerCase())).map(searchedPlayers => {
          return(
            <tr key={searchedPlayers.name}>
              <td>{searchedPlayers.name}</td>
            </tr>
          );
        })
    }

【问题讨论】:

  • 为什么要把 redux 状态复制到本地状态?
  • 另外,你不会在任何地方打电话给.filter
  • 1.我认为在 React 状态下过滤列表会更容易。 2.我尝试输入.filter,但出现错误
  • 不,它更难......如果你只是调用this.props.players.filter你根本不需要任何本地状态......你甚至不需要使用组件类只需使用一个函数
  • ... 这是一个数组

标签: javascript reactjs react-redux filtering


【解决方案1】:
this.state.players.filter(player => player.name.includes(this.state.player))

如果你想映射它们而不是仅仅过滤状态......

this.state.players.filter(player => 
player.name.includes(this.state.player)).map(searchedPlayers => {
  return(
    <tr key={searchedPlayers.name}>
      <td>{searchedPlayers.name}</td>
    </tr>
  );
})

请注意,您也可以直接从 props 渲染而不设置状态,(以避免每次用户键入时重新渲染)通过替换

this.state.players

this.props.players

【讨论】:

  • 这个答案如果没问题。如果提到没有理由将 redux 状态复制到本地状态,那就更好了
  • INSERTUSERSEARCHHERE 更改为 this.state.player - 已正确使用/设置
【解决方案2】:

你不需要对 props 使用复制状态,你也可以使用this.props.players

getPlayers() {
  if(this.state.player) {
    return this.state.players.filter(player => player.name.includes(this.state.player))
  } else {
    return this.state.players
  }
}

然后您可以调用let collection = getPlayers(); 并将播放器映射到html 内容。

【讨论】:

    【解决方案3】:

    你可以使用过滤器:

    renderList(){
        const { player } = this.state
        const { players } = this.props
        // ignore user's typing case
        const iPlayer = player.toLowerCase()
        const filteredPlayers = players.filter(p => 
           p.name.toLowerCase().includes(iPlayer)
        )
        // now, map to the filteredPlayers
    

    【讨论】:

      猜你喜欢
      • 2017-06-14
      • 2014-08-14
      • 1970-01-01
      • 2017-09-24
      • 1970-01-01
      • 2022-01-12
      • 2022-01-08
      • 1970-01-01
      相关资源
      最近更新 更多