javascript - 使用 React JS 的 HandleClick 不使用绑定(bind)

标签 javascript reactjs

这是我的 react 代码

handleRemoveClick = material => {
  let idMaterial = material.material_id;
  if(this.state.operation === 0){
    this.setState( { previewShowAlertRemoveProduct : true })
    }
    this.setState( { operation : 1, material_id : idMaterial})
}



handleEndOrder(){
  if(this.state.operation === 0){
    this.setState( { previewShowAlertRemoveProduct : true })
    }
    this.setState( { operation : 2})
}

这是为了渲染 View :

  <button class="btn btn-xs btn-danger pull-right"  onClick={e => this.handleRemoveClick(material)}>x</button>

如何解决这个问题? onClick 它根本不起作用 我在构造函数中绑定(bind)了 handleEndOrder

最佳答案

因为你没有指定按钮的类型,默认是submit,你需要指定type="button"或者使用 e.preventDefault() 喜欢

<button class="btn btn-xs btn-danger pull-right" type="button" onClick={e => this.handleRemoveClick(material)}>x</button>

handleRemoveClick = (e, material) => {
  e.preventDefault();
  let idMaterial = material.material_id;
  if(this.state.operation === 0){
    this.setState( { previewShowAlertRemoveProduct : true })
    }
    this.setState( { operation : 1, material_id : idMaterial})
}
<button class="btn btn-xs btn-danger pull-right"  onClick={e => this.handleRemoveClick(e, material)}>x</button>

关于javascript - 使用 React JS 的 HandleClick 不使用绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50063155/

相关文章:

javascript - pg-promise - 错误运算符不存在 : bigint = bigint[]

javascript - 在破坏数组时如何阻止 Eclipse 在列上对齐?

reactjs - 当我有多个 reducer 时,如何在用户注销期间删除所有状态

javascript - 在 React js 中收到错误 "this.props.sumOfPrices is not a function"?

reactjs - key 在 ReactJs 的子组件中不能用作 prop

javascript - Typescript:在 Visual Studio 2013 中编译为多个 .js 文件

javascript - 使用 forEach() 循环数组最大数

javascript - 如何将天数添加到今天的日期?

javascript - Graphql 查询为关系文档返回 null,查询中缺少字段但存在于 primsa admin 中

javascript - 循环 this.props.children 我如何测试他们的类型?