`

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

 

更多实例应用扫码体验:

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics