網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
immutable
它是一款代替?zhèn)鹘y(tǒng)拷貝方式的第三方庫(kù)
優(yōu)勢(shì):
- 在新對(duì)象上操作不會(huì)影響原對(duì)象的數(shù)據(jù)
- 性能好
安裝使用
1.下載 npm i immutable
2.導(dǎo)入 import {Map} from 'immutable'
Map
語(yǔ)法:Map(對(duì)象)
賦值:set("屬性名","新值")
取值:get("屬性名")
toJS()
轉(zhuǎn)回普通對(duì)象
import React, { Component } from 'react'; /** * 1.下載 npm i immutable * 2.導(dǎo)入 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轉(zhuǎn)回普通對(duì)象 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中對(duì)象中的對(duì)象還要套上Map
可以通過(guò)map的get
方法獲取值向組件傳值.,從而完美的解決了組件的無(wú)效刷新
shouldComponentUpdate
進(jìn)行判斷決定是否需要刷新
import React, { Component } from 'react'; import {Map} from 'immutable' class Immu03 extends Component { state={ info:Map({ name:"碰磕", age:20, hobbit:Map({ likeone:"運(yùn)動(dòng)", liketwo:"學(xué)習(xí)" }) }) } render() { return ( <div> Immu03 <button onClick={()=>{this.setState({ info:this.state.info.set("name","學(xué)習(xí)啊啊啊") })}}>修改</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
可以使用數(shù)組的屬性方法
import React, { Component } from 'react'; import {List} from 'immutable' var arr=List([1,231,1]) let arr2=arr.push(4)//不會(huì)影響原對(duì)象結(jié)構(gòu) let arr3=arr2.concat([12,323,123]) console.log(arr,arr2,arr3); class Immu04 extends Component { state={ favor:List(['吃飯','睡覺(jué)','學(xué)習(xí)','運(yùn)動(dòng)']) } render() { return ( <div> Immu04 { this.state.favor.map(item=>{ return <li key={item}>{item}</li> }) } </div> ); } } export default Immu04;
實(shí)現(xiàn)個(gè)人修改案例
import { List,Map } from 'immutable'; import React, { Component } from 'react'; class Immu05 extends Component { state={ info:Map({ name:"碰磕", location:Map({ province:"江西", city:"吉安" }), hobbit:List(['睡覺(jué)','學(xué)習(xí)','敲鍵盤']) }) } render() { return ( <div> <h1>編輯個(gè)人信息</h1> <div> <button onClick={()=>{ this.setState({ info:this.state.info.set("name","愛(ài)學(xué)習(xí)").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;
通過(guò)fromJS、setIn、updateIn進(jìn)行改進(jìn)
-
fromJS
將普通對(duì)象轉(zhuǎn)換為Immutable -
setIn()
深度賦值,參數(shù)一為數(shù)組形式填寫需要修改的,參數(shù)二為修改后的值 -
updateIn()
深度修改,參數(shù)一為數(shù)組形式填寫需要修改的,參數(shù)二為回調(diào)函數(shù)(參數(shù)為原對(duì)象)
import { fromJS } from 'immutable'; import React, { Component } from 'react'; class Immu06 extends Component { state={ info:fromJS({ name:"碰磕", location:{ province:"江西", city:"吉安" }, hobbit:['睡覺(jué)','學(xué)習(xí)','敲鍵盤'] }) } render() { return ( <div> <h1>編輯個(gè)人信息</h1> <div> <button onClick={()=>{ this.setState({ info:this.state.info.setIn(["name"],"愛(ài)學(xué)習(xí)").setIn(["location","city"],"南昌")//setIn("參數(shù)一為數(shù)組","修改后的值") }) }}>修改</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(需要修改的對(duì)象,回調(diào)函數(shù)(參數(shù)為原對(duì)象)) this.setState({ info:this.state.info.updateIn(["hobbit"],(list)=>list.splice(index,1)) }) }}>刪除</button></li>) } </div> </div> ); } } export default Immu06;
這樣就學(xué)費(fèi)了Immutable~
原文鏈接:https://blog.csdn.net/m_xiaozhilei/article/details/125453118
相關(guān)推薦
- 2022-07-08 go語(yǔ)言代碼生成器code?generator使用示例介紹_Golang
- 2022-12-15 Android入門之Toast的使用教程_Android
- 2022-12-26 層次分析法在matlab上的實(shí)現(xiàn)方式_python
- 2022-10-18 Python語(yǔ)言中的Selenium環(huán)境搭建_python
- 2023-07-08 前端使用FileReader讀中文會(huì)亂碼
- 2022-11-09 Python有序容器的?sort?方法詳解_python
- 2023-04-20 使用replaceAll()方法實(shí)現(xiàn)數(shù)字千分位逗號(hào)分隔
- 2022-04-07 WPF實(shí)現(xiàn)數(shù)據(jù)綁定_實(shí)用技巧
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支