網站首頁 編程語言 正文
immutable
它是一款代替傳統拷貝方式的第三方庫
優勢:
- 在新對象上操作不會影響原對象的數據
- 性能好
安裝使用
1.下載 npm i immutable
2.導入 import {Map} from 'immutable'
Map
語法:Map(對象)
賦值:set("屬性名","新值")
取值:get("屬性名")
toJS()
轉回普通對象
import React, { Component } from 'react'; /** * 1.下載 npm i immutable * 2.導入 import {Map} from 'immutable' */ import {Map} from 'immutable' var obj={ name:"碰磕", age:20 } var objImmu=Map(obj); var newobjImmu=objImmu.set("name","pengkeStudy"); // console.log(objImmu,newobjImmu) //get('屬性')獲取值 console.log(objImmu.get("name"),newobjImmu.get("name")); //toJS轉回普通對象 console.log(objImmu.toJS(),newobjImmu.toJS()); /* //寫法一 class Immu02 extends Component { state={ info:Map({ name:"碰磕", age:20 }) } render() { return ( <div> Immu02 <button onClick={()=>{ this.setState({ info:this.state.info.set('name',"pengkeStudy").set('age',1000) }) }}>修改它們的值</button> {this.state.info.get("name")}---- {this.state.info.get("age")} </div> ); } }*/ //寫法二 class Immu02 extends Component { state={ info:{ name:"碰磕", age:20 } } render() { return ( <div> Immu02 <button onClick={()=>{ let old=Map(this.state.info) let newImmu=old.set("name","pengkeStudy").set("age",10000) this.setState({ info:newImmu.toJS() }) }}>修改它們的值</button> {this.state.info.name}---- {this.state.info.age} </div> ); } } export default Immu02;
嵌套Map
Map中對象中的對象還要套上Map
可以通過map的get
方法獲取值向組件傳值.,從而完美的解決了組件的無效刷新
shouldComponentUpdate
進行判斷決定是否需要刷新
import React, { Component } from 'react'; import {Map} from 'immutable' class Immu03 extends Component { state={ info:Map({ name:"碰磕", age:20, hobbit:Map({ likeone:"運動", liketwo:"學習" }) }) } render() { return ( <div> Immu03 <button onClick={()=>{this.setState({ info:this.state.info.set("name","學習啊啊啊") })}}>修改</button> {this.state.info.get("name")} <Child hobbit={this.state.info.get("hobbit")} ></Child> </div> ); } } class Child extends Component{ shouldComponentUpdate(nextProps,nextState){ //判斷hobbit的值是否更新 if(this.props.hobbit===nextProps.hobbit){ return false; } return true; } render(){ return( <div>Child</div> ); } componentDidUpdate(){ console.log("子組件更新了"); } } export default Immu03;
List
可以使用數組的屬性方法
import React, { Component } from 'react'; import {List} from 'immutable' var arr=List([1,231,1]) let arr2=arr.push(4)//不會影響原對象結構 let arr3=arr2.concat([12,323,123]) console.log(arr,arr2,arr3); class Immu04 extends Component { state={ favor:List(['吃飯','睡覺','學習','運動']) } render() { return ( <div> Immu04 { this.state.favor.map(item=>{ return <li key={item}>{item}</li> }) } </div> ); } } export default Immu04;
實現個人修改案例
import { List,Map } from 'immutable'; import React, { Component } from 'react'; class Immu05 extends Component { state={ info:Map({ name:"碰磕", location:Map({ province:"江西", city:"吉安" }), hobbit:List(['睡覺','學習','敲鍵盤']) }) } render() { return ( <div> <h1>編輯個人信息</h1> <div> <button onClick={()=>{ this.setState({ info:this.state.info.set("name","愛學習").set("location",this.state.info.get("location").set("city","南昌")) }) }}>修改</button> {this.state.info.get("name")} <br /> {this.state.info.get("location").get("province")}- {this.state.info.get("location").get("city")} { this.state.info.get("hobbit").map((item,index)=><li key={item}>{item}<button onClick={()=>{ // console.log(index); this.setState({ info:this.state.info.set("hobbit",this.state.info.get("hobbit").splice(index,1)) }) }}>刪除</button></li>) } </div> </div> ); } } export default Immu05;
通過fromJS、setIn、updateIn進行改進
-
fromJS
將普通對象轉換為Immutable -
setIn()
深度賦值,參數一為數組形式填寫需要修改的,參數二為修改后的值 -
updateIn()
深度修改,參數一為數組形式填寫需要修改的,參數二為回調函數(參數為原對象)
import { fromJS } from 'immutable'; import React, { Component } from 'react'; class Immu06 extends Component { state={ info:fromJS({ name:"碰磕", location:{ province:"江西", city:"吉安" }, hobbit:['睡覺','學習','敲鍵盤'] }) } render() { return ( <div> <h1>編輯個人信息</h1> <div> <button onClick={()=>{ this.setState({ info:this.state.info.setIn(["name"],"愛學習").setIn(["location","city"],"南昌")//setIn("參數一為數組","修改后的值") }) }}>修改</button> {this.state.info.get("name")} <br /> {this.state.info.get("location").get("province")}- {this.state.info.get("location").get("city")} { this.state.info.get("hobbit").map((item,index)=><li key={item}>{item}<button onClick={()=>{ // console.log(index); // this.setState({ // info:this.state.info.setIn(["hobbit",index],"") // }) //updateIn(需要修改的對象,回調函數(參數為原對象)) this.setState({ info:this.state.info.updateIn(["hobbit"],(list)=>list.splice(index,1)) }) }}>刪除</button></li>) } </div> </div> ); } } export default Immu06;
這樣就學費了Immutable~
原文鏈接:https://blog.csdn.net/m_xiaozhilei/article/details/125453118
相關推薦
- 2022-09-25 自動微分----pytorch中的梯度運算與反向傳播函數(預備知識)
- 2022-07-15 C++?DLL動態庫的創建與調用(類庫,隱式調用)_C 語言
- 2022-03-14 Linux磁盤格式化和掛載(linux服務器硬盤掛載步驟)
- 2022-12-04 如何對csv文件數據分組,并用pyecharts展示_python
- 2022-03-27 ASP.NET?HttpRequest類介紹_基礎應用
- 2022-11-03 python數據分析基礎知識之shape()函數的使用教程_python
- 2022-07-10 CSS解決未知高度垂直居中
- 2022-05-20 ASP.NET?MVC模式簡介_基礎應用
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支