欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

React替換傳統(tǒng)拷貝方法的Immutable使用

 更新時(shí)間:2023年02月06日 10:04:58   作者:碰磕  
Immutable.js出自Facebook,是最流行的不可變數(shù)據(jù)結(jié)構(gòu)的實(shí)現(xiàn)之一。它實(shí)現(xiàn)了完全的持久化數(shù)據(jù)結(jié)構(gòu),使用結(jié)構(gòu)共享。所有的更新操作都會(huì)返回新的值,但是在內(nèi)部結(jié)構(gòu)是共享的,來(lái)減少內(nèi)存占用

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());
/*
//寫(xiě)法一
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>
        );
    }
}*/
//寫(xiě)法二
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í)','敲鍵盤(pán)'])
        })
    }
    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ù)組形式填寫(xiě)需要修改的,參數(shù)二為修改后的值
  • updateIn()深度修改,參數(shù)一為數(shù)組形式填寫(xiě)需要修改的,參數(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í)','敲鍵盤(pán)']
        })
    }
    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~

到此這篇關(guān)于React替換傳統(tǒng)拷貝方法的Immutable使用的文章就介紹到這了,更多相關(guān)React Immutable內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • React+echarts?(echarts-for-react)?實(shí)現(xiàn)中國(guó)地圖及省份切換功能

    React+echarts?(echarts-for-react)?實(shí)現(xiàn)中國(guó)地圖及省份切換功能

    這篇文章主要介紹了React+echarts?(echarts-for-react)?畫(huà)中國(guó)地圖及省份切換,有足夠的地圖數(shù)據(jù),可以點(diǎn)擊到街道,示例我只出到市級(jí),本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì)需要的朋友可以參考下
    2022-11-11
  • react?源碼中位運(yùn)算符的使用詳解

    react?源碼中位運(yùn)算符的使用詳解

    這篇文章主要為大家詳細(xì)介紹了react?位運(yùn)算符,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-03-03
  • React反向代理與CSS模塊化的使用案例

    React反向代理與CSS模塊化的使用案例

    這篇文章主要介紹了React反向代理與CSS模塊化的使用案例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧
    2023-02-02
  • React?Hooks useReducer?逃避deps組件渲染次數(shù)增加陷阱

    React?Hooks useReducer?逃避deps組件渲染次數(shù)增加陷阱

    這篇文章主要介紹了React?Hooks?之?useReducer?逃避deps后增加組件渲染次數(shù)的陷阱詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • antd form表單如何處理自定義組件

    antd form表單如何處理自定義組件

    這篇文章主要介紹了antd form表單如何處理自定義組件問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • React?中的?JS?報(bào)錯(cuò)及容錯(cuò)方案

    React?中的?JS?報(bào)錯(cuò)及容錯(cuò)方案

    這篇文章主要為大家介紹了React?中的?JS?報(bào)錯(cuò)及容錯(cuò)方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • mobx在react hooks中的應(yīng)用方式

    mobx在react hooks中的應(yīng)用方式

    這篇文章主要介紹了mobx在react hooks中的應(yīng)用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • React事件綁定的方式詳解

    React事件綁定的方式詳解

    react事件綁定時(shí)。this并不會(huì)指向當(dāng)前DOM元素。往往使用bind來(lái)改變this指向,今天通過(guò)本文給大家介紹React事件綁定的方式,感興趣的朋友
    2021-07-07
  • react嵌套路由實(shí)現(xiàn)TabBar的實(shí)現(xiàn)

    react嵌套路由實(shí)現(xiàn)TabBar的實(shí)現(xiàn)

    本文主要介紹了react嵌套路由實(shí)現(xiàn)TabBar的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • React如何動(dòng)態(tài)控制偽元素樣式

    React如何動(dòng)態(tài)控制偽元素樣式

    這篇文章主要介紹了React如何動(dòng)態(tài)控制偽元素樣式問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11

最新評(píng)論