




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第Reactimmer與ReduxToolkit使用教程詳解目錄1.immer1.1setState結(jié)合immer使用1.2useState結(jié)合immer使用1.3immer和redux集合2.ReduxToolkit
1.immer
概述:
它和immutable相似的,實(shí)現(xiàn)了操作對(duì)象的數(shù)據(jù)共享,可以?xún)?yōu)化性能。它實(shí)現(xiàn)的原理使用es6的Proxy完成的。小巧,沒(méi)有像immutable哪樣有新數(shù)據(jù)類(lèi)型,而是使用js類(lèi)型。
安裝:
yarnaddimmer@9
1.1setState結(jié)合immer使用
簡(jiǎn)單使用:
importReactfrom'react'
//把源數(shù)據(jù)使用Proxy進(jìn)行代理處理,后面就可以精準(zhǔn)的去找到變化的數(shù)據(jù)
import{produce}from'immer'
conststate={
count:1
//進(jìn)行代理后,并修改
//draft就是代理對(duì)象,它就是state代理對(duì)象
constnewState=produce(state,draft={
draft.count++
console.log(newState.count)//2
constApp=()={
returndiv/div
exportdefaultApp
使用immer進(jìn)行proxy代理,源數(shù)據(jù)中只有變化的了的數(shù)據(jù)才更新,沒(méi)有變化則共享,這樣做可以提高性能:
importReactfrom'react'
//把源數(shù)據(jù)使用Proxy進(jìn)行代理處理,后面就可以精準(zhǔn)的去找到變化的數(shù)據(jù)
import{produce}from'immer'
constbaseState={
arr:[1,2,3],
obj:{id:1,name:'張三'}
//使用immer進(jìn)行proxy代理,源數(shù)據(jù)中只有變化的了的數(shù)據(jù)才更新,沒(méi)有變化則共享,提高性能
constnewState=produce(baseState,draft={
draft.arr.push(4)
//當(dāng)前只修改數(shù)組,對(duì)象沒(méi)有修改,共享
console.log('arr',newState.arr===baseState.arr)//false
console.log('obj',newState.obj===baseState.obj)//true
constApp=()={
returndiv/div
exportdefaultApp
使用immer優(yōu)化setState使用:
importReact,{Component}from'react'
import{produce}from'immer'
//優(yōu)化setState更新數(shù)據(jù)
classAppextendsComponent{
state={
count:100,
carts:[
{id:1,name:'aa',num:1},
{id:2,name:'bb',num:2}
addCount=()={
//原來(lái)的寫(xiě)法
//this.setState(state=({count:state.count+1}))
//使用immer來(lái)優(yōu)化setState操作
this.setState(
produce(draft={
//不用返回,返加不允許,draft它是一個(gè)代理對(duì)象,修改后,它能監(jiān)聽(tīng)到數(shù)據(jù)的變化,更新視圖
draft.count++
render(){
return(
div
h3{this.state.count}/h3
buttonthis.addCount}++++++/button
hr/
{this.state.carts.map((item,index)=(
likey={item.id}
span{}/span
span/span
span
{item.num}--
button
()={
this.setState(
produce(draft={
//draft它就是當(dāng)前的this.state
draft.carts[index].num++
++++
/button
/span
/li
/div
exportdefaultApp
1.2useState結(jié)合immer使用
importReact,{useState}from'react'
import{produce}from'immer'
constApp=()={
//普通的數(shù)字,不是proxy代理,proxy代理的是對(duì)象
//如果它是一個(gè)普通值,沒(méi)有必要使用immer來(lái)完成優(yōu)化操作
let[count,setCount]=useState(100)
//對(duì)象類(lèi)型才是使用immer工作的場(chǎng)景
let[carts,setCarts]=useState([
{id:1,name:'aa',num:1},
{id:2,name:'bb',num:2}
return(
div
h3{count}/h3
button
()={
//之前的寫(xiě)法
//setCount(v=v+1)
setCount(produce(draft=draft+1))
+++count+++
/button
hr/
button
()={
setCarts(
produce(draft={
draft.push({id:Date.now(),num:1,name:'aaaa--'+Date.now()})
添加商品
/button
{carts.map((item,index)=(
likey={item.id}
span{}/span
span/span
span
{item.num}--
button
()={
setCarts(
produce(draft={
//不能返回,寫(xiě)上去感覺(jué)就像在vue的感覺(jué)
draft[index].num++
++++
/button
/span
span
()={
setCarts(
produce(draft={
draft.splice(index,1)
/span
/li
/div
exportdefaultApp
1.3immer和redux集合
redux/index.js:
import{createStore}from'redux'
import{produce}from'immer'
constinitState={
count:100
//以前的寫(xiě)法
/*constreducer=(state=initState,{type,payload})={
if('add'===type){
//深復(fù)制
return{...state,count:state.count+payload}
returnstate
constreducer=produce((draft,{type,payload})={
//寫(xiě)法就和vuex中的mutation中的寫(xiě)法一樣的,簡(jiǎn)化了
//操作數(shù)據(jù)無(wú)需深復(fù)制,提升性能
if('add'===type)draft.count+=payload
},initState)
exportdefaultcreateStore(reducer)
前端頁(yè)面:
importReactfrom'react'
import{useSelector,useDispatch}from'react-redux'
constApp=()={
constdispatch=useDispatch()
constcount=useSelector(state=state.count)
return(
div
h3{count}/h3
button
()={
dispatch({type:'add',payload:2})
++++
/button
/div
exportdefaultApp
2.ReduxToolkit
概述:
它開(kāi)箱即用的高效Redux開(kāi)發(fā)工具集,是redux新的庫(kù),也是官方推薦今后在項(xiàng)目中使用的redux庫(kù),內(nèi)置了immer、redux-thunk和redux-devtools。
安裝:
yarnadd@reduxjs/toolkitreact-redux
使用:
前臺(tái)頁(yè)面:
importReactfrom'react'
import{useSelector,useDispatch}from'react-redux'
constApp=()={
constdispatch=useDispatch()
constcount=useSelector(state=state.count)
return(
div
h3{count}/h3
button
()={
dispatch({type:'add',payload:2})
++++
/button
/div
exportdefaultApp
redux入口文件:
import{configureStore}from'@reduxjs/toolkit'
//上來(lái)它就是分模塊,在項(xiàng)目中,所以的數(shù)據(jù)一定是分模塊來(lái)管理的
importcountfrom'./modules/count'
importuserfrom'./modules/users'
exportdefaultconfigureStore({
reducer:{
count,
user
})
同步操作(count.js):
import{createSlice}from'@reduxjs/toolkit'
//同步操作
constcountSlice=createSlice({
//命名空間名稱(chēng),比redux中更好,redux沒(méi)有
//它的名稱(chēng)要和入口文件中configureStore中的reducer配置對(duì)象中的key名稱(chēng)要一致
name:'count',
//初始化數(shù)據(jù)源
initialState:{
num:10
//修改數(shù)據(jù)源的方法集合
reducers:{
//組件中派發(fā)dispatch(setNum(2))//2就會(huì)給payload
//state它就是proxy對(duì)象[immer]
setNum(state,{payload}){
state.num+=payload
//導(dǎo)出給在組件中調(diào)用
exportconst{setNum}=countSlice.actions
//把當(dāng)前模塊的reducer導(dǎo)入,集合到大的reducer中
exportdefaultcountSlice.reducer
異步操作(users.js):
import{createSlice,createAsyncThunk}from'@reduxjs/toolkit'
//方案2
//寫(xiě)在上面,進(jìn)行網(wǎng)絡(luò)請(qǐng)求
exportconstfetchUser=createAsyncThunk('user/fetchUser',asyncpage={
//實(shí)現(xiàn)異步數(shù)據(jù)獲取
letret=await(awaitfetch('/api/userspage='+page)).json()
//return中的數(shù)據(jù)就是返回到state中的users中的數(shù)據(jù)一定要return
returnret.data
//異步操作
constuserSli
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 車(chē)輛風(fēng)險(xiǎn)押金合同協(xié)議
- 還貸免責(zé)協(xié)議書(shū)模板
- 建筑設(shè)計(jì)與施工合同及協(xié)議
- 歷史文化保護(hù)與傳承的試題研究
- 《當(dāng)代生產(chǎn)管理策略》課件
- 豬肉購(gòu)銷(xiāo)合同
- 民政合作協(xié)議書(shū)
- 語(yǔ)培課程合同協(xié)議書(shū)模板
- 返建房房屋合同補(bǔ)充協(xié)議
- 車(chē)場(chǎng)使用協(xié)議書(shū)范本
- 建筑工程技術(shù)專(zhuān)業(yè)《建筑結(jié)構(gòu)》課程標(biāo)準(zhǔn)
- 2024年廣東普通專(zhuān)升本《公共英語(yǔ)》完整版真題
- 綠化養(yǎng)護(hù)工作日記錄表
- 《養(yǎng)老護(hù)理員》-課件:協(xié)助老年人穿脫簡(jiǎn)易矯形器
- 間質(zhì)性肺炎 護(hù)理
- 銀行客戶維護(hù)方案
- 雨水收集和利用使用雨水進(jìn)行農(nóng)業(yè)灌溉和城市供水
- 物業(yè)公司保潔員培訓(xùn)課件
- 《中小學(xué)書(shū)法教育指導(dǎo)綱要》解讀
- 一年二十四節(jié)氣規(guī)律
- 校園消防安全知識(shí)教育課件
評(píng)論
0/150
提交評(píng)論