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

react中使用antd及immutable示例詳解

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

一、react中使用antd組件庫

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

運行命令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

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

2.1 深拷貝和淺拷貝的關系

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

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

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

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

安裝輸入命令npm i immutable:

2.3 immutable的Map使用

map使用(map只能套一層,如果屬性還是對象或者數(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.獲取值:普通對象
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>
    )
  }
}

可以看到多個值時,immutable可以鏈式操作。

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 實際場景formJS

在實際開發(fā)中我們的state中數(shù)據(jù)結構一般來自后端返回的,那么我們將使用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')
                    })
                }}>改變標題</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

在學習React的路上,如果你覺得本文對你有所幫助的話,那就請關注點贊評論三連吧,謝謝,你的肯定是我寫博的另一個支持。

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

相關文章

  • React從react-router路由上做登陸驗證控制的方法

    React從react-router路由上做登陸驗證控制的方法

    本篇文章主要介紹了React從react-router路由上做登陸驗證控制的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • 詳解React?Native中如何使用自定義的引用路徑

    詳解React?Native中如何使用自定義的引用路徑

    這篇文章主要為大家介紹了React?Native中如何使用自定義的引用路徑詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-10-10
  • React報錯Element type is invalid解決案例

    React報錯Element type is invalid解決案例

    這篇文章主要為大家介紹了React報錯Element type is invalid解決案例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • React?中使用?Redux?的?4?種寫法小結

    React?中使用?Redux?的?4?種寫法小結

    這篇文章主要介紹了在?React?中使用?Redux?的?4?種寫法,Redux 一般來說并不是必須的,只有在項目比較復雜的時候,比如多個分散在不同地方的組件使用同一個狀態(tài),本文就React使用?Redux的相關知識給大家介紹的非常詳細,需要的朋友參考下吧
    2022-06-06
  • ReactNative支付密碼輸入框實現(xiàn)詳解

    ReactNative支付密碼輸入框實現(xiàn)詳解

    這篇文章主要為大家介紹了ReactNative支付密碼輸入框實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11
  • Input標簽自動校驗功能去除實現(xiàn)

    Input標簽自動校驗功能去除實現(xiàn)

    這篇文章主要為大家介紹了Input標簽的自動拼寫檢查功能去除實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-07-07
  • react-navigation 如何判斷用戶是否登錄跳轉到登錄頁的方法

    react-navigation 如何判斷用戶是否登錄跳轉到登錄頁的方法

    本篇文章主要介紹了react-navigation 如何判斷用戶是否登錄跳轉到登錄頁的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • 詳解react hooks組件間的傳值方式(使用ts)

    詳解react hooks組件間的傳值方式(使用ts)

    本文主要介紹了react hooks組件間的傳值方式(使用ts),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • React組件間傳值及跨組件通信詳解

    React組件間傳值及跨組件通信詳解

    這篇文章主要介紹了React組件間傳值及跨組件通信的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-09-09
  • React Native 中添加自定義字體的方法

    React Native 中添加自定義字體的方法

    這篇文章主要介紹了如何在 React Native 中添加自定義字體,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-08-08

最新評論