3 回答

TA貢獻1794條經(jīng)驗 獲得超7個贊
您當前的實現(xiàn)正在執(zhí)行所謂的“短路”,基本上只要第一個值計算為真值,第二個值就會被返回,否則它可能會返回undefined。
要修復您的實現(xiàn),它應(yīng)該是這樣的:
<table className="tableList tableList--space">
<thead>{this.tableHead()}</thead>
<tbody>
{this.state.products.map((item) => (
<React.Fragment>
{this.tableBody(item)}
{this.tableBodyComplements(item)}
</React.Fragment>
)
)}
</tbody>
</table>

TA貢獻1810條經(jīng)驗 獲得超5個贊
運算&&符實際上不會同時返回兩者。如果第一個參數(shù)為 true,則返回第二個參數(shù),否則返回 false。例如:
function Example() {
return (
<div>
{"Word1" && "Word2"}
</div>
) // this displays ONLY "Word2", since "Word1" is not a false value.
}
要解決此問題,請將它們包裝在片段中:
{this.state.products.map((item) =>
<React.Fragment>
{this.tableBody(item)}
{this.tableBodyComplements(item)}
</React.Fragment>
)}

TA貢獻1811條經(jīng)驗 獲得超6個贊
在 javascript 中,任何形式的表達式
a && b
或者
a || b
只會計算為或 a。b從來沒有兩者兼而有之。事實上,甚至不清楚評估“兩者a和b”的一般含義是什么。
在你的例子中,你想將兩個元素渲染為 JSX - 所以只需將它們放在一起,包裝在 a 中,F(xiàn)ragment這樣它就是一個合法的 React 元素:
{this.state.products.map((item) =>
<React.Fragment>
{this.tableBody(item)}
{this.tableBodyComplements(item)}
</React.Fragment
)}
添加回答
舉報