0

目标我让它通过 onclick 与路由显示不同的 HTML

Render() 中没有逻辑 const contents = this.state.data.map(item => (

这是我正在努力的逻辑

<button id={item.Member_ID} type="button"
  {` ${this.isExist(item.Member_ID) ? <Link to='surveysai/'>
      <button type="button" className='btn btn-success'>SAI</button>  </Link>             
    : ${onClick={(e) => this.downloadUser(item.Member_ID, e)}}`}
    className={`btn ${this.isExist(item.Member_ID) ? "btn-success" : "btn-warning"}`} > {this.isExist(item.Member_ID) ? "SAI" : "Ready for Download"}</button>

这适用于按钮类:

<button id={item.Member_ID} type="button"
 className={`btn ${this.isExist(item.Member_ID) ? "btn-success" : "btn-warning"}`} > {this.isExist(item.Member_ID) ? "SAI" : "Ready for Download"}</button>   

以下是更改为条件之前的旧代码,以下不是我想要的代码,仅供参考。

点击

<button id={item.Member_ID} type="button" onClick={(e) => this.downloadUser(item.Member_ID,e)} 
      className={() => this.checkItem(item.Member_ID)}>Ready for Download</button>

链接路由重定向

<Link to='surveysai/'>
    <button type="button" className="btn btn-success">SAI</button>                   
</Link>
4

1 回答 1

1

你为什么不有条件地渲染Link或按钮。

{this.isExist(item.Member_ID) ? (
  <Link to='surveysai/'>
      <button type="button" className='btn btn-success'>SAI</button>  
  </Link>
) : (
  <button 
    onClick={(e) => this.downloadUser(item.Member_ID, e)}
    className={`btn ${this.isExist(item.Member_ID) ? "btn-success" : "btn-warning"}`}> {this.isExist(item.Member_ID) ? "SAI" : "Ready for Download"} . 
  </button>
)}

Link此外,假设这是一个 React RouterLink组件,您实际上不需要在 a 内渲染按钮。您可以将类名作为道具传递,例如:

<Link to="/wherever" className="btn btn-success" />

于 2019-09-30T20:29:43.947 回答