在这里。因为四年后我对 React 有了更多的了解,而且这仍然引起人们的关注,所以我想我会用我现在的做法来更新它。
SavedSearches.js
import React from 'react'
import { SearchList } from './SearchList'
let data = [
    {index: 0, name: "a string", url: 'test.com/?search=string'},
    {index: 1, name: "a name", url: 'test.com/?search=name'},
    {index: 2, name: "return all", url: 'test.com/?search=all'}
  ];
let startingIndex = data.length;
export class SavedSearches extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            name: '',
            url: '',
            index: startingIndex,
            data: data
        }
        this.deleteSearch=this.deleteSearch.bind(this)
    }
    deleteSearch(deleteThis) {
        console.log(deleteThis);
        let newData = this.state.data.filter( searchItem => searchItem.index !== deleteThis.index )
        this.setState({
            data: newData
        })
    }
    render() {
        return (
            <div className="search-container">
                <SearchList data={this.state.data} onDelete={this.deleteSearch}/>
            </div>
        )
    }
}
在这里,我创建了一个名为的方法deleteSearch,该方法将对象作为参数。然后它.filter在this.state.data数组上运行以创建一个包含所有不满足条件的项目的新数组。条件检查数据数组中每个对象的 id 是否与参数的 id 匹配。如果是这样,那么它就是被删除的那个。创建的新数组.filter被设置为一个名为 的变量newData,然后我用该newData数组更新状态。
然后我将此方法传递给SearchList名为 .prop的组件中的组件onDelete。
此方法也绑定在构造函数 using 中,.bind()以便在该方法沿组件树向下传递时this将引用正确this的方法。
搜索列表.js
import React from 'react'
import { SearchItem } from './SearchItem'
export class SearchList extends React.Component {
    render() {
      let searchItems = this.props.data.map((item, i) => {
        return (
          <SearchItem index={i} searchItem={item} url={item.url} onDelete={this.props.onDelete}>
            {item.name}
          </SearchItem>
        );
      });
      return (
        <ul>
          {searchItems}
        </ul>
      );
    }
}
我的deleteSearch方法只是通过这里的组件树。SearchList接收方法作为propsthis.props.onDelete并将其传递给SearchItem.
这里的另一个主要关键是 map 函数中的参数作为 props: 传递searchItem={item}。这将允许通过 props 访问整个当前对象;如果你还记得的话,我的deleteSearch函数接受一个对象作为参数。
搜索项.js
import React from 'react'
export class SearchItem extends React.Component {
    constructor(props) {
        super(props);
        this.handleDelete=this.handleDelete.bind(this)
    }
    handleDelete() {
        this.props.onDelete(this.props.searchItem)
    }
    render() {
      return (
        <li key={this.props.index}> {/* Still getting a console error over this key */}
          <a href={this.props.url} title={this.props.name}>
            {this.props.children}
          </a>
           ({this.props.url})
          <button onClick={this.handleDelete} value={this.props.index}><i className="fa fa-times"></i>
          </button>
        </li>
      );
    }
  };
现在我的方法到达了它将被使用的地方。我创建了一个处理程序方法,handleDelete并在内部deleteSearch使用this.props.onDelete. 然后我将正在单击的列表项的对象传递给它this.props.searchItem。  
为了这个工作,当用户点击,我不得不添加一个onClick事件侦听器调用我的处理方法,就像这样:onClick={this.handleDelete}。最后一步是this.handleDelete在SearchItem构造函数方法中绑定。
现在,单击按钮将从this.state.data数组中删除该项目。有关如何向数组添加项目的示例,请参阅我的存储库