網站首頁 編程語言 正文
一、react中使用antd組件庫
運行命令create-react-app antd-react創建新項目:
運行命令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對象時都會創建一個新的不可變的對象,在新對象上操作并不會影響到原對象的數據。
2.1 深拷貝和淺拷貝的關系
1、Object.assign或者... 只是一級屬性賦值,比淺拷貝多拷貝了一層而已。
2、const obj1 = JSON.parse(JSON.stringify(obj)) 數組,對象都好用(缺點:不能有字段為undefined)。
2.2 immutable優化性能方式
immutable實現的原料是Persistent Data Structure(持久化數據結構),也就是使用舊數據創建新數據時,要保準舊數據同時可用且不變。同時為了避免deepCopy把所有節點都復制一遍帶來的性能損耗,immutable使用了structural sharing(結構共享),即如果對象樹中一個節點發生變化,只修改這個節點和受它影響的父節點,其它節點則進行共享。
安裝輸入命令npm i immutable:
2.3 immutable的Map使用
map使用(map只能套一層,如果屬性還是對象或者數組的話就再套一層):
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
在實際開發中我們的state中數據結構一般來自后端返回的,那么我們將使用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)
})
})
}}>改變數組</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的路上,如果你覺得本文對你有所幫助的話,那就請關注點贊評論三連吧,謝謝,你的肯定是我寫博的另一個支持。
原文鏈接:https://juejin.cn/post/7129120942155890718
相關推薦
- 2022-09-16 selenium.chrome寫擴展攔截或轉發請求功能_C#教程
- 2023-11-13 matplotlib按照論文要求繪圖并保存pdf格式
- 2022-04-03 基于QT5實現一個時鐘桌面_C 語言
- 2022-10-19 C#如何提取經緯度文件中的經緯度數據_C#教程
- 2022-06-10 FreeRTOS實時操作系統隊列基礎_操作系統
- 2022-11-28 golang進程在docker中OOM后hang住問題解析_Golang
- 2022-09-15 Go語言操作redis數據庫的方法_Golang
- 2023-01-23 Electron打包React生成桌面應用方法詳解_React
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支