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

react中使用antd及immutable示例詳解

 更新時(shí)間:2022年08月08日 09:40:00   作者:你華還是你華  
這篇文章主要為大家介紹了react中使用antd及immutable示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

一、react中使用antd組件庫(kù)

運(yùn)行命令create-react-app antd-react創(chuàng)建新項(xiàng)目:

運(yùn)行命令npm i antd安裝:

使用:

import React from 'react'
import ReactDOM from 'react-dom'
import 'antd/dist/antd.css';
import { Button } from 'antd';
ReactDOM.render(
    <div>
        <Button type="primary">Primary Button</Button>
    </div>, 
    document.getElementById("root"));

二、Immutable

每次修改一個(gè)immutable對(duì)象時(shí)都會(huì)創(chuàng)建一個(gè)新的不可變的對(duì)象,在新對(duì)象上操作并不會(huì)影響到原對(duì)象的數(shù)據(jù)。

2.1 深拷貝和淺拷貝的關(guān)系

1、Object.assign或者... 只是一級(jí)屬性賦值,比淺拷貝多拷貝了一層而已。

2、const obj1 = JSON.parse(JSON.stringify(obj)) 數(shù)組,對(duì)象都好用(缺點(diǎn):不能有字段為undefined)。

2.2 immutable優(yōu)化性能方式

immutable實(shí)現(xiàn)的原料是Persistent Data Structure(持久化數(shù)據(jù)結(jié)構(gòu)),也就是使用舊數(shù)據(jù)創(chuàng)建新數(shù)據(jù)時(shí),要保準(zhǔn)舊數(shù)據(jù)同時(shí)可用且不變。同時(shí)為了避免deepCopy把所有節(jié)點(diǎn)都復(fù)制一遍帶來(lái)的性能損耗,immutable使用了structural sharing(結(jié)構(gòu)共享),即如果對(duì)象樹中一個(gè)節(jié)點(diǎn)發(fā)生變化,只修改這個(gè)節(jié)點(diǎn)和受它影響的父節(jié)點(diǎn),其它節(jié)點(diǎn)則進(jìn)行共享。

安裝輸入命令npm i immutable:

2.3 immutable的Map使用

map使用(map只能套一層,如果屬性還是對(duì)象或者數(shù)組的話就再套一層):

import React from 'react'
import { createRoot } from 'react-dom/client';
import 'antd/dist/antd.css'
import { Button } from 'antd'
import {Map} from 'immutable'
var obj = {
    name: 'immutable'
}
var oldObj = Map(obj)
console.log(oldObj)
// 更改值
var newObj = oldObj.set('name', 'react')
// 1.獲取值:get獲取
console.log(oldObj.get('name'), newObj.get('name'))
// 2.獲取值:普通對(duì)象
console.log(oldObj.toJS(), newObj.toJS())
const container = document.getElementById('root')
const root = createRoot(container)
root.render(
    <div>
        <Button type="primary">Primary Button</Button>
    </div>
)

效果:

state中使用:

import React, { Component } from 'react'
import { Map } from 'immutable'
export default class imMap extends Component {
  state = {
    info: Map({
      name: 'immutable',
      key: 100
    })
  }
  render() {
    return (
      <div>
        <button onClick={() => {
          this.setState({
            info: this.state.info.set('name', 'react').set('key', 101)
          })
        }}>onclick</button>
        {this.state.info.get('name')} -- 
        {this.state.info.get('key')}
      </div>
    )
  }
}

可以看到多個(gè)值時(shí),immutable可以鏈?zhǔn)讲僮鳌?/p>

2.4 immutable的List使用

import React, { Component } from 'react'
import {List} from 'immutable'
var arr = List([1,2,3])
var arr1 = arr.push(4)
var arr2 = arr1.concat([5])
console.log(arr.toJS(), arr1.toJS(), arr2.toJS())
export default class ImList extends Component {
  render() {
    return (
      <div>ImList</div>
    )
  }
}

效果:

2.5 實(shí)際場(chǎng)景formJS

在實(shí)際開發(fā)中我們的state中數(shù)據(jù)結(jié)構(gòu)一般來(lái)自后端返回的,那么我們將使用formJS:

import React, { Component } from 'react'
import { fromJS } from 'immutable'
export default class ImFromJs extends Component {
    state = {
        info: fromJS({
            list: ['1', '2', '3'],
            obj: {
                name: 'immutable',
                key: 100,
            }
        })
    }
    render() {
        return (
            <div>ImFromJs
                <div><button onClick={() => {
                    this.setState({
                        info: this.state.info.setIn(['obj', 'name'], 'react')
                    })
                }}>改變標(biāo)題</button></div>
                <div><button onClick={() => {
              this.setState({
                  info: this.state.info.updateIn(['list'], (oldList) => {
                    return oldList.push(oldList._tail.array.length + 1)
                  })    
              })
          }}>改變數(shù)組</button></div>
                <div>{this.state.info.getIn(['obj', 'name'])}</div>
                <div>
                    <ul>
                        {
                            this.state.info.get('list').map((item, index) => {
                                return <li key={index}>{item} 
                                <button onClick={() => {
                                    this.setState({
                                        info: this.state.info.updateIn(['list'], (oldList) => {
                                            return oldList.splice(index, 1)
                                        })
                                    })
                                }}>del</button>
                                </li>
                            })
                        }
                    </ul>
                </div>
            </div>
        )
    }
}

效果:

三、redux中使用immutable

在學(xué)習(xí)React的路上,如果你覺(jué)得本文對(duì)你有所幫助的話,那就請(qǐng)關(guān)注點(diǎn)贊評(píng)論三連吧,謝謝,你的肯定是我寫博的另一個(gè)支持。

以上就是react中使用antd及immutable示例詳解的詳細(xì)內(nèi)容,更多關(guān)于react使用antd immutable的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • React?Hooks之usePolymerAction抽象代碼結(jié)構(gòu)設(shè)計(jì)理念

    React?Hooks之usePolymerAction抽象代碼結(jié)構(gòu)設(shè)計(jì)理念

    這篇文章主要為大家介紹了React?Hooks之usePolymerAction抽象代碼結(jié)構(gòu)設(shè)計(jì)理念,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • React?Hooks的useState、useRef使用小結(jié)

    React?Hooks的useState、useRef使用小結(jié)

    React Hooks 是 React 16.8 版本引入的新特性,useState和useRef是兩個(gè)常用的Hooks,本文主要介紹了React?Hooks的useState、useRef使用,感興趣的可以了解一下
    2024-01-01
  • React中的JSX??{?}的使用詳解

    React中的JSX??{?}的使用詳解

    這篇文章主要介紹了React中的JSX{?}的使用,React使用JSX來(lái)替代常規(guī)的JavaScript,JSX可以理解為的JavaScript語(yǔ)法擴(kuò)展,它里面的標(biāo)簽申明要符合XML規(guī)范要求,對(duì)React?JSX使用感興趣的朋友一起看看吧
    2022-08-08
  • React 原理詳解

    React 原理詳解

    這篇文章主要介紹了深入理解react的原理,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2021-10-10
  • Modal.confirm是否違反了React模式分析

    Modal.confirm是否違反了React模式分析

    這篇文章主要為大家介紹了Modal.confirm是否違反了React模式分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • 一文詳解如何React中實(shí)現(xiàn)插槽

    一文詳解如何React中實(shí)現(xiàn)插槽

    這篇文章主要為大家詳細(xì)介紹了如何在React中實(shí)現(xiàn)插槽,文中的示例代碼講解詳細(xì),對(duì)我們的學(xué)習(xí)或工作具有一定的借鑒價(jià)值,需要的可以了解一下
    2023-03-03
  • React實(shí)現(xiàn)合成事件的源碼分析

    React實(shí)現(xiàn)合成事件的源碼分析

    React?中的事件,是對(duì)原生事件的封裝,叫做合成事件。抽象出一層合成事件,是為了做兼容,抹平不同瀏覽器之間的差異。本文將從事件綁定和事件觸發(fā)角度,帶大家解讀下源碼,感興趣的可以了解一下
    2022-12-12
  • ReactNative 之FlatList使用及踩坑封裝總結(jié)

    ReactNative 之FlatList使用及踩坑封裝總結(jié)

    本篇文章主要介紹了ReactNative 之FlatList使用及踩坑封裝總結(jié),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-11-11
  • React組件實(shí)例三大屬性state props refs使用詳解

    React組件實(shí)例三大屬性state props refs使用詳解

    這篇文章主要為大家介紹了React組件實(shí)例三大屬性state props refs使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • 解決React報(bào)錯(cuò)`value` prop on `input` should not be null

    解決React報(bào)錯(cuò)`value` prop on `input` should&

    這篇文章主要為大家介紹了React報(bào)錯(cuò)`value` prop on `input` should not be null解決方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12

最新評(píng)論

鄢陵县| 云林县| 丁青县| 柞水县| 汕尾市| 罗江县| 栾城县| 双桥区| 北流市| 呼图壁县| 广灵县| 桐柏县| 云和县| 太康县| 九寨沟县| 岢岚县| 宁城县| 北安市| 怀仁县| 东辽县| 响水县| 林州市| 武城县| 专栏| 股票| 文昌市| 莒南县| 临汾市| 犍为县| 凌海市| 华坪县| 名山县| 大丰市| 伊宁县| 扬州市| 鹤庆县| 辽源市| 庆城县| 灵丘县| 禹州市| 县级市|