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,乔乐共享)