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

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

僅當在 useEffect 內部加載數(shù)據(jù)后計數(shù)大于 0 時才顯示計數(shù)

僅當在 useEffect 內部加載數(shù)據(jù)后計數(shù)大于 0 時才顯示計數(shù)

紅顏莎娜 2021-09-04 17:27:31
在useEffect,notifications是從數(shù)據(jù)庫中獲取的:useEffect(() => {    if (props.loading != true) {         props.fetchNotifications(currentUserId, submissionId)    }}, [])在返回語句的某處,我有以下代碼在通知列表上應用過濾器并檢查剩余列表的長度是否大于零。然后,如果是,我使用相同的過濾器語句來計算這次顯示的計數(shù)。但是,我進行了兩次計算,創(chuàng)建了冗余代碼。我想知道在 React 中是否有解決這個問題的方法。   props.notifications &&   <div className="col-sm-4">       <div className="mb-3">           <h5 className="text-primary bg-light pl-1 pt-2 pb-2">               <i className="fa fa-bell-o"></i> &nbsp;Notifications               <span className="badge badge-pill badge-primary small ml-2">                   {                       Object.keys(props.notifications)                           .filter(function (id) {                               return props.notifications[id].isDiscussionType == false &&                                   props.notifications[id].isRead == false                            }).length > 0 &&                       <span>{                           Object.keys(props.notifications)                               .filter(function (id) {                                   return props.notifications[id].isDiscussionType == false &&                                       props.notifications[id].isRead == false                                }).length} new                       </span>                   }               </span>           </h5>           <NotificationList isDiscussionType={false} />       </div>
查看完整描述

2 回答

?
有只小跳蛙

TA貢獻1824條經驗 獲得超8個贊

您可以將結果存儲在一個常量中,以便只執(zhí)行filter一次操作:


const notificationsLength = props.notifications ? Object.keys(props.notifications).filter(function (id) {

    return props.notifications[id].isDiscussionType == false &&

        props.notifications[id].isRead == false

}).length : 0


return (

    <span className="badge badge-pill badge-primary small ml-2">

        {


            notificationsLength &&

            <span>{notificationsLength} new</span>

        }

    </span>

)


查看完整回答
反對 回復 2021-09-04
?
溫溫醬

TA貢獻1752條經驗 獲得超4個贊

您可以將其提取出來并創(chuàng)建一個變量,例如:


const filteredNotifications = Object.keys(props.notifications)

                                    .filter(id => props.notifications[id].isDiscussionType == false 

                                               && props.notifications[id].isRead == false })

然后你可以做類似的事情


<span className="badge badge-pill badge-primary small ml-2">

 {

  filteredNotifications.length > 0 &&

  (<span>{filteredNotifications.length} new </span>)

 }

</span>


查看完整回答
反對 回復 2021-09-04
  • 2 回答
  • 0 關注
  • 220 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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