gpt4 book ai didi

javascript - ReactJS 需要根据逻辑有条件地显示不同的 onclick 和 Link

转载 作者:行者123 更新时间:2023-12-01 00:35:24 25 4
gpt4 key购买 nike

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

Render() 中没有逻辑 constcontents = 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>

最佳答案

为什么不有条件地渲染 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 Router Link成分。您可以将类名作为 props 传递,例如:

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

关于javascript - ReactJS 需要根据逻辑有条件地显示不同的 onclick 和 Link,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58174810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com