最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

immutable

它是一款代替?zhèn)鹘y(tǒng)拷貝方式的第三方庫

優(yōu)勢:

  • 在新對象上操作不會影響原對象的數(shù)據(jù)
  • 性能好

安裝使用

1.下載 npm i immutable

2.導(dǎo)入 import {Map} from 'immutable'

Map

語法:Map(對象)

賦值:set("屬性名","新值")

取值:get("屬性名")

toJS()轉(zhuǎn)回普通對象

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)回普通對象
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:"學(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)//不會影響原對象結(jié)構(gòu)
let arr3=arr2.concat([12,323,123])
console.log(arr,arr2,arr3);
class Immu04 extends Component {
    state={
        favor:List(['吃飯','睡覺','學(xué)習(xí)','運動'])
    }
    render() {
        return (
            <div>
                Immu04
                {
                    this.state.favor.map(item=>{
                        return <li key={item}>{item}</li>
                    })
                }
            </div>
        );
    }
}
export default Immu04;

實現(xiàn)個人修改案例

import { List,Map } from 'immutable';
import React, { Component } from 'react';
class Immu05 extends Component {
    state={
        info:Map({
            name:"碰磕",
            location:Map({
                province:"江西",
                city:"吉安"
            }),
            hobbit:List(['睡覺','學(xué)習(xí)','敲鍵盤'])
        })
    }
    render() {
        return (
            <div>
                <h1>編輯個人信息</h1>
                <div>
                    <button onClick={()=>{
                        this.setState({
                            info:this.state.info.set("name","愛學(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;

通過fromJS、setIn、updateIn進行改進

  • fromJS將普通對象轉(zhuǎn)換為Immutable
  • setIn()深度賦值,參數(shù)一為數(shù)組形式填寫需要修改的,參數(shù)二為修改后的值
  • updateIn()深度修改,參數(shù)一為數(shù)組形式填寫需要修改的,參數(shù)二為回調(diào)函數(shù)(參數(shù)為原對象)
import { fromJS } from 'immutable';
import React, { Component } from 'react';
class Immu06 extends Component {
    state={
        info:fromJS({
            name:"碰磕",
            location:{
                province:"江西",
                city:"吉安"
            },
            hobbit:['睡覺','學(xué)習(xí)','敲鍵盤']
        })
    }
    render() {
        return (
            <div>
                <h1>編輯個人信息</h1>
                <div>
                    <button onClick={()=>{
                        this.setState({
                            info:this.state.info.setIn(["name"],"愛學(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(需要修改的對象,回調(diào)函數(shù)(參數(shù)為原對象))
                            this.setState({
                                info:this.state.info.updateIn(["hobbit"],(list)=>list.splice(index,1))
                            })
                        }}>刪除</button></li>)
                    }
                </div>
            </div>
        );
    }
}
export default Immu06;

這樣就學(xué)費了Immutable~

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

相關(guān)文章

  • antd+react中upload手動上傳單限制上傳一張

    antd+react中upload手動上傳單限制上傳一張

    本文主要介紹了antd+react中upload手動上傳單限制上傳一張,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • 詳解前端路由實現(xiàn)與react-router使用姿勢

    詳解前端路由實現(xiàn)與react-router使用姿勢

    本篇文章主要介紹了詳解前端路由和react-router使用姿勢,詳細的介紹了react-router的用法,有興趣的可以了解一下
    2017-08-08
  • 如何創(chuàng)建自己的第一個React 頁面

    如何創(chuàng)建自己的第一個React 頁面

    React是用于構(gòu)建用戶界面的JavaScript庫,本文主要介紹了如何創(chuàng)建自己的第一個React頁面,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • React簡便獲取經(jīng)緯度信息的方法詳解

    React簡便獲取經(jīng)緯度信息的方法詳解

    在現(xiàn)代的Web應(yīng)用程序中,獲取用戶的地理位置信息是一項常見的需求,本文我們將介紹如何在React應(yīng)用程序中簡便地獲取用戶的經(jīng)緯度信息,需要的可以參考下
    2023-11-11
  • 用react實現(xiàn)一個簡單的scrollView組件

    用react實現(xiàn)一個簡單的scrollView組件

    這篇文章主要給大家介紹一下如何用 react 實現(xiàn)一個簡單的 scrollView組件,文中有詳細的代碼示例,具有一定的參考價值,需要的朋友可以參考下
    2023-07-07
  • react的嚴格模式和解決react useEffect執(zhí)行兩次問題

    react的嚴格模式和解決react useEffect執(zhí)行兩次問題

    文章總結(jié):本文詳細探討了React中useEffect執(zhí)行兩次的問題,主要歸因于React的嚴格模式,嚴格模式在開發(fā)模式下會故意重復(fù)調(diào)用一些生命周期方法,以幫助開發(fā)者發(fā)現(xiàn)潛在的問題,包括不安全的生命周期、過時的ref API、廢棄的findDOMNode方法、意外的副作用等
    2025-01-01
  • 三分鐘搞懂react-hooks及實例代碼

    三分鐘搞懂react-hooks及實例代碼

    React?Hooks是今年最勁爆的新特性真的毫不夸張。如果你也對react感興趣,或者正在使用react進行項目開發(fā),請抽出點時間閱讀下此文
    2022-03-03
  • React使用api的方式封裝彈窗的示例代碼

    React使用api的方式封裝彈窗的示例代碼

    在現(xiàn)代開發(fā)中的彈窗樣式,經(jīng)常會是底部一個叉號樣式的彈窗,但是目前組件庫中并無類似彈窗組件,本文小編給大家介紹了React使用api的方式封裝彈窗的示例,感興趣的小伙伴跟著小編一起來看看吧
    2024-09-09
  • 2個奇怪的react寫法

    2個奇怪的react寫法

    大家好,我卡頌。雖然React官網(wǎng)用大量篇幅介紹最佳實踐,但因JSX語法的靈活性,所以總是會出現(xiàn)奇奇怪怪的React寫法。本文介紹2種奇怪(但在某些場景下有意義)的React寫法。也歡迎大家在評論區(qū)討論你遇到過的奇怪寫法
    2023-03-03
  • react-beautiful-dnd 實現(xiàn)組件拖拽功能

    react-beautiful-dnd 實現(xiàn)組件拖拽功能

    這篇文章主要介紹了react-beautiful-dnd 實現(xiàn)組件拖拽功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-08-08

最新評論

三台县| 洞头县| 内黄县| 和林格尔县| 太湖县| 和静县| 耒阳市| 航空| 五台县| 通化市| 张家界市| 洱源县| 扶风县| 井陉县| 桂东县| 五寨县| 塘沽区| 中卫市| 普安县| 台山市| 修武县| 赫章县| 慈利县| 谢通门县| 威远县| 金乡县| 浦县| 江油市| 东阿县| 慈利县| 乐山市| 会泽县| 临海市| 永泰县| 左权县| 安岳县| 三明市| 昌宁县| 泰州市| 集贤县| 隆德县|