日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

react props數(shù)據(jù)更改傳到方法里進(jìn)行渲染html,當(dāng)父組件改變props改變傳入函數(shù)參數(shù)沒有改變

作者:有問題請?jiān)u論我改 更新時(shí)間: 2022-04-05 編程語言

解決方法使用JSON.parse(JSON.stringify(variableObject))深拷貝一下需變動的數(shù)組,因?yàn)槿绻豢截恗ap改變了數(shù)組(數(shù)組里的元素為引用類型),而react禁止改變props

  getOnlineData = (roommates, onlineRoommates) => {
    const newRoommates =JSON.parse(JSON.stringify(roommates))
    return (newRoommates.map(item => {
      onlineRoommates.map(element => {
        if (`${item.user_type}:${item.user_id}` === element.userId) {
          item.isOnline = true
          item.hasVideo = element.hasVideo
          item.hasAudio = element.hasAudio
        }
      })
      return this.renderItem(item)
    }))
  }
  render () {
    const { roommates, onlineRoommates } = this.props
 
    return (
      <div className={styles.container}>
        {this.getOnlineData(roommates, onlineRoommates)}
      </div>
    )
  }

原文鏈接:https://blog.csdn.net/qq_26889291/article/details/122825536

欄目分類
最近更新