gpt4 book ai didi

javascript - ReactJS 中按钮和普通文本之间的条件渲染

转载 作者:行者123 更新时间:2023-12-03 01:05:33 25 4
gpt4 key购买 nike

我有一个对象数组,表示defect,现在如果缺陷的状态是打开的,那么它应该显示为按钮,并且它应该读取关闭缺陷,如果它是关闭的,那么它应该显示为按钮它应该只是提到已关闭。因此,这里的 statusRender 就是问题所在,并且现在按最后一列中的预期工作。无法弄清楚我错过了什么。有线索吗?

render() {

if (defect.defect_status == 'open') {
statusRender = <button key={index} data-id={defect.id} onClick={() => this.showAlert(defect.defect_id)}>{defect.defect_status}</button>;
} else {
statusRender = { defect.defect_status };
}
return (
<div>
<table className="table table-bordered table-hover">
<thead>
<tr>
<th>Defect ID</th>
<th>Category</th>
<th>Description</th>
<th>Priority</th>
<th>Status</th>
</tr>
</thead>
<tbody>
{this.state.defectList.map((defect, index) => {
return (
<tr key={index}>
<td> {defect.defect_id} </td>
<td>{defect.defect_category}</td>
<td>{defect.defect_description}</td>
<td>{defect.defect_priority}</td>
<td> {statusRender}
</td>
</tr>
);
})
}
</tbody>
</table>
</div>
)
}

最佳答案

这是一个范围问题,您无法在 map 函数之外声明缺陷

{this.state.defectList.map((defect,index) => {
return (
<tr key={index}>
<td> {defect.defect_id} </td>
<td>{defect.defect_category}</td>
<td>{ defect.defect_description}</td>
<td>{ defect.defect_priority}</td>
<td>
{
defect.defect_status === 'open'
? <button key={index} data-id={defect.id} onClick = {() => this.showAlert(defect.defect_id)}>{defect.defect_status}</button>;
: defect.defect_status;
}
</td>
</tr>
);
})
}

关于javascript - ReactJS 中按钮和普通文本之间的条件渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52421845/

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