React-StepPitGuide
  • 封面
  • 前言
  • 基础知识
    • 环境配置
      • WebPack 前端打包工具
      • Babel 编译器
      • Node 环境配置
    • 版本说明
    • JSX
    • 组件
    • Child组件
    • 多组件嵌套
    • 组件生命周期
      • 组件生命周期(React16之前)
    • State属性
    • Props属性
    • 类组件
      • React PropTypes
      • React 可复用组件
      • React 事件与数据的双向绑定
      • React 组件DOM操作
      • React 独立组件间共享 Mixins ⚠
      • React 高阶组件(HOC)
        • 继承方式的高阶组件
        • 代理方式的高阶组件
      • React 无状态组件与PureComponent
      • React 容器组件和展示组件
      • setState
    • 函数组件
      • React 函数子组件
      • React Hook
        • useState
        • useEffect
    • 组件样式
      • CSS內联式样
      • CSS模块化
    • 受控与非受控组件
  • 进阶知识
  • React Context
  • React 顶级API
  • React Portal 传送门
  • 应用知识
    • 路由
      • React-Router2
      • React-router4
        • React-router4 结合React-Redux
    • 服务端渲染
    • 性能优化
      • React 组件优化
        • 定制shouldComponentUpdate
        • PureComponent
        • immutablejs存在的意义和使用
      • Redux性能优化
      • React同构
    • 项目应用
      • React 文件组织方式
      • React 模块接口
      • React 合并reducer
      • React 问题
  • 状态管理
    • Flux
    • Redux
      • Redux 使用
      • Redux 异步
      • Redux Chrome插件使用
      • Redux React-Redux
      • Redux applyMiddleware
      • Redux 结合React
      • Redux 自制Redux
      • Redux 自制React-Redux
      • Redux 自制thunk中间件
      • Redux 自制arr中间件
    • Mobx
      • Mobx 思想
    • RXJS
    • immutable
  • 源码思考
    • 简述
    • SimpleReact
    • 虚拟DOM
      • 虚拟DOM概念(React 16前)
      • 虚拟DOM思考(React 16前)
    • 生命周期
    • 事件
    • Fiber
      • 数据结构
      • Scheduler 调度
      • Concurrent 异步渲染
    • Hooks
      • useState
由 GitBook 提供支持
在本页
  • immutablejs存在的意义和使用
  • React中使用
  • immutable.js

这有帮助吗?

  1. 应用知识
  2. 性能优化
  3. React 组件优化

immutablejs存在的意义和使用

immutablejs存在的意义和使用

先来看一个事例,就是在JS中对比两个对象

let obj = {name:1}
let obj1 = {name:1}

console.log(obj == obj1)//false

在JS中无法通过 == 对比两对象是否相等

通过一个对比函数,来对比两个对象是否相等

function compareObj(obj1,obj2){
  if(obj1==obj2){
    return true
  }
  if(Object.keys(obj1).length!==Object.keys(obj2).length){
    return false
  }
  for(let k in obj){
    if(obj1[k]!==obj2[k]){
      return false
    }
  }
  return true
}
  • 查看obj的属性采用for in

通过这样的对比可以实现对比两个对象,但是如果obj内部出现了多层对象

let obj = {
  name:'1',
  {
    title:'react'
  }
}

如果是这样,我们就必须要考虑深比较

function compareObj(obj1,obj2){
  if(obj1==obj2){
    return true
  }
  if(Object.keys(obj1).length!==Object.keys(obj2).length){
    return false
  }
  for(let k in obj){
    if(typeof obj1[k]==='object'){
      return compareObj(obj1[k],obj2[k])
    }else if(obj1[k]!==obj2[k]){
      return false
    }
  }
  return true
}

React中使用

class Demo extends React.Component { 
  shouldComponentUpdate(nextProps, nextState){
  if(compareObj(nextProps,this.props)){
    return false
  }
  return true
}
  render() {
    return (<h2>I am Demo,{this.props.title}</h2>)
  }
}

export default App

React中对比props,递归对比,复杂度会很高,会严重影响性能

React官方建议,只做浅层比较,所以

function compareObj(obj1,obj2){
  if(obj1==obj2){
    return true
  }
  if(Object.keys(obj1).length!==Object.keys(obj2).length){
    return false
  }
  for(let k in obj){
    if(obj1[k]!==obj2[k]){
      return false
    }
  }
  return true
}

immutable.js

import React from 'react'
import { Map } from 'immutable'

class App extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      num: 1,
      title: 'tyrmars',
      name: 'zhang'
    }
    this.handleClick = this.handleClick.bind(this)
    this.handleTitle = this.handleTitle.bind(this)
  }
  handleClick() {
    this.setState({
      num: this.state.num + 1
    })
  }
  handleTitle() {
    this.setState({
      title:this.state.title + '!'
    })
  }
  render() {
    console.log('render');
    return (<div>
      <h2>I am App{this.state.num}</h2>
      <button onClick={this.handleClick}>button 1</button>
      <button onClick={this.handleTitle}>btnTitle</button>
      <Demo title={this.state.title}></Demo>
    </div>)
  }
}

let obj = Map({
  'name':'yueyue',
  'course':Map({'name':'react'})
})

let obj1 = obj.set('name','zhang')

let obj2 = Map({
  'name':'yueyue',
  'course':Map({'name':'react'})
})

console.log(obj1===obj);
console.log(obj2.course===obj.course);


class Demo extends React.Component {
  shouldComponentUpdate(nextProps, nextState){
    if (nextProps.title===this.props.title) {
      return false
    }
    return true
  }
  render() {
    return (<h2>I am Demo {this.props.title}</h2>)
  }
}

export default App
上一页PureComponent下一页Redux性能优化

最后更新于4年前

这有帮助吗?

查看Github上的 - Facebook的一个库()

immutable.js
介绍