React state从数组中动态移除子项item

1、当我们动态操作state的适合,遇到移除一个子项,需要更新数组的state

// set state
this.state = {
  files: []
}

// add item
this.setState({
      files: [
        ...this.state.files,
        fileInfo
      ]
    });


// Remove item
removePic(index) {
    this.setState({
      files: this.state.files.filter((_, i) => i !== index)
    })
  }

<span onClick={this.removePic.bind(this, index)} />


// show items
{state.files.map((file, index) => (
              <li key={index}>
                <span onClick={this.removePic.bind(this, index)} />
              </li>
            ))}



有疑问或技术交流,扫描公众号一起讨论学习。

更多React在线学习访问:http://each.sinaapp.com/react/index.html

猜你喜欢

转载自qiaolevip.iteye.com/blog/2288018