zoukankan      html  css  js  c++  java
  • react优化部分immutable

    immutable  结合componentShouldComponent  

    state = {

      counter:{number:0}

    }

    shouldComponentUpdate(nextProps,nextState){
        for(let key in nextState){
          if(this.state[key] !== nextState[key]){
            return true
          }
        }
        return false
      }
    存在以下问题:
    1.如果counter对象属性变了,但是对象没变,无法更新
    2.如果counter对象变了,但是counter属性没变  却更新了
     
    该更得不更新,不该更新得更了   这就是问题
     
    浅比较:速度非常快,比较地址,缺点:无法准确的感知内容的变化
     
    深比较:性能太差    return  !_.isEqual(this.state,nextState)    lodash的深克隆
     
    目的:根据内容变化刷新,性能好    import {is,Map} from 'immutable'
    state = {counter:Map({number:0})}
    变化时 : let counter=this.state.counter.update('number',val=>val+1)   
     
     
     
     
        if(Object.keys(this.state).length !== Object.keys(nextState).length)return true
        for(let key in nextState){
          if(!is(this.state[key],nextState[key])){
            return true
          }
        }
        return false
    其实根本问题在于借助immutable的is方法进行比较了,我们是浅比较却做了深比较的事
     
     
     
  • 相关阅读:
    ajax专题
    luogu P1346 电车 最短路
    luogu P1462 通往奥格瑞玛的道路 最短路
    luogu P1328 生活大爆炸版石头剪刀布
    luogu P1315 联合权值 枚举
    luogu P1156 垃圾陷阱 背包问题
    luogu P1217 回文质数 枚举
    luogu P3650 滑雪课程设计 枚举
    luogu1209 修理牛棚 贪心
    luogu P1223 排队接水 贪心
  • 原文地址:https://www.cnblogs.com/MDGE/p/14606697.html
Copyright © 2011-2022 走看看