我想要点击删除按钮的图像ID

时间:2018-05-02 04:15:40

标签: reactjs

**我希望在onclick的reactjs中显示特定图像的ID。我们有一个数组,其中包含有关图像的所有数据,即图像[]。如何传递特定图像的id以将其删除为deleteimage函数。任何建议或建议都将受到欢迎**

class Image extends Component {
  constructor() {
    super();
    this.state = { images: [] };
  }


  componentDidMount() {
    let result = apiGateway.getImages();
    if (result != null) {
      result.then((value) => {
        this.setState({ images: value.data.data });
      });
    }
  }

   deleteImage=()=>{

    let result = apiGateway.removeImage(??);
  }



renderImage(value,index){
  let str = value.imagePath.split('/').pop();
  let string = str.slice(0,-5);
  return(
    <div key={index}>
     <p>Title: {value.title}</p>
     <p>Description: {value.description}</p>
    <button onClick={this.deleteImage}>DELETE</button>
    <button>UPDATE</button>
    <img className='image' src= {'http://localhost:3000/'+ string } width='100' height='100' alt='nature'/>
    <br/><br/>
    </div>
    );
}


  render() {

    return(  
                 <div>
                      <div>{
                        this.state.images.map((image,index)=>{

                          return this.renderImage(image,index);
                        })
                      }
                      </div>
                </div>
    );
  }

}

export default Image;

1 个答案:

答案 0 :(得分:0)

尝试发送valuedeleteImage(value)功能:

<button onClick={this.deleteImage.bind(this, value)}>DELETE</button>

OR

<button onClick={(e) => this.deleteImage(value, e)}>DELETE</button>

请查看更多here