当前位置:网站首页>Immutable學習之路----告別傳統拷貝
Immutable學習之路----告別傳統拷貝
2022-06-25 04:35:00 【碰磕】
首發:CSDN碰磕,學無止境
多雲30°
The greatest test of courage on earth is to bear defaet without losing heart.
2022/6/24
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
將普通對象轉換為ImmutablesetIn()
深度賦值,參數一為數組形式填寫需要修改的,參數二為修改後的值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~
边栏推荐
- GBase 8s的封锁技术的基本介绍
- Can Navicat directly operate the Android database SQLite
- OBS Browser+浏览器的基本使用
- UCLA | generative pre training for black box optimization
- 什么是持久化?redis 持久化中的RDB和AOF是什么?
- Introduction to the isolation level of gbase 8s
- Gbase 8s stored procedure execution and deletion
- Summary of various problems encountered by cocos2d-x
- Cascading deletion of gbase 8s
- 深度学习——几种学习类型
猜你喜欢
Intel 13th generation core showed its true colors for the first time: 68mb cache improved significantly
cnpm : 无法加载文件 C:\Users\Administrator\AppData\Roaming\npm\cnpm.ps1,因为在此系统上禁止运行脚本。
Laravel document sorting 4. Controller
CTF_ Web: basic 12 questions WP of attack and defense world novice zone
CTF_ Web: advanced problem WP (5-8) of attack and defense world expert zone
Gbase 8s index b+ tree
Cnpm: unable to load file c:\users\administrator\appdata\roaming\npm\cnpm PS1 because running scripts is prohibited on this system.
Unity Quad culls shaders with back faces and transparent parts
PHP extracts and analyzes table contents, and collects bidding information
Nodejs 通过Heidisql连接mysql出现ER_BAD_DB_ERROR: Unknown database 'my_db_books'
随机推荐
How to screen out words related to products and eliminate invalid words accurately
我的IC之旅——资深芯片设计验证工程师成长——“胡”说IC工程师完美进阶
Laravel document sorting 4. Controller
Nodejs connects to MySQL through heidisql, and ER appears_ BAD_ DB_ ERROR: Unknown database 'my_ db_ books'
Immutable学习之路----告别传统拷贝
SQL注入详解
Data view for gbase 8s
CTF_ Web:8-bit controllable character getshell
Retrofit source code analysis
OBS Browser+浏览器的基本使用
php开发支付宝支付功能之扫码支付流程图
Anaconda installation +tensorflow installation +keras installation +numpy installation (including image and version information compatibility issues)
SQL injection details
js的sort()函数
@Requestbody solution get parameter is null
Synchronous and asynchronous functions (callback function, promise, generator, async/await)
515. 在每个树行中找最大值 / 剑指 Offer II 095. 最长公共子序列
GBASE 8s 索引B+树
GBASE 8s的并行操作问题场景描述
Finereport (sail soft) handling the problem that the histogram data label is blocked