[英]React / Javascript If statement inside map
我正在嘗試制作一個表,該表將根據從map函數派生的屬性具有不同顏色的行:
var tableData = this.state.data.map(attribute =>
{if(attribute.duplicate.toString()== "true")
return <tr className="table-success">;
else
return <tr className="table-warning">;
}
<td> {count++} </td>
...
<td> {attribute.duplicate.toString()} </td>
</tr>
);
現在, var tableData
傳遞到實際表:
<div>
<div>
<table className="table">
<tr className="thead-inverse">
<th> # </th>
...
<th> duplicate </th>
</tr>
{tableData}
</table>
</div>
</div>
但是我收到以下錯誤:
[INFO] Module build failed: SyntaxError: C:/Users/.../all.js: Unterminated JSX contents (78:14)
[INFO]
[INFO] 76 |
[INFO] 77 | </div>
[INFO] > 78 | </div>
[INFO] | ^
任何想法是什么問題,還是實現我正在嘗試的另一種方式?
只需將條件邏輯移到className
或將其分配給變量,然后在tr
內使用它即可。
var tableData = this.state.data.map(attribute =>
<tr className={attribute.duplicate.toString() == "true" ? "table-success" : "table-warning"}>
<td> {count++} </td>
...
<td> {attribute.duplicate.toString()} </td>
</tr>
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.