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方法进行比较了,我们是浅比较却做了深比较的事
 
 
 
原文地址:https://www.cnblogs.com/MDGE/p/14606697.html