第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

只有一個函數(shù)得到結(jié)果

只有一個函數(shù)得到結(jié)果

炎炎設(shè)計 2023-10-20 16:08:00
我有這兩個函數(shù)被調(diào)用,但只有第二個函數(shù)顯示在桌子上。<table className="tableList tableList--space">     <thead>{this.tableHead()}</thead>     <tbody>          {this.state.products.map((item) =>               this.tableBody(item) && this.tableBodyComplements(item)          )}     </tbody></table>功能:tableBody = (item) => (    <tr key={item.title}>        <td className="text-left">            {item.title}        </td>        <td className="text-left"> - </td>        <td className="text-right">R$ {item.price}</td>        <td className="text-center">{item.quantity}X</td>        <td className="text-right">R$ {item.total}</td>        <td className="text-left"></td>    </tr>);tableBodyComplements = (item) => (    <tr>        <td className="text-right">             {item.ComplementCategories.map(aux => {                return aux.Complements[0].title            })}         </td>        <td className="text-right"> - </td>        <td className="text-right"> - </td>        <td className="text-right">             {item.ComplementCategories.map(aux => {                return aux.Complements[0].quantity            })}         </td>        <td className="text-right">R$ {item.total}</td>        <td className="text-right"></td>    </tr>);為什么只有第二個得到了想要的結(jié)果?我不知道是否只調(diào)用了第二個,或者它與表中的第一個重疊,我怎樣才能更好地理解并修復它?
查看完整描述

3 回答

?
慕田峪9158850

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>


查看完整回答
反對 回復 2023-10-20
?
森欄

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>

 )}


查看完整回答
反對 回復 2023-10-20
?
楊魅力

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

)}


查看完整回答
反對 回復 2023-10-20
  • 3 回答
  • 0 關(guān)注
  • 148 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號