React+valtio響應(yīng)式狀態(tài)管理
Valtio 是一個(gè)很輕量級(jí)的響應(yīng)式狀態(tài)管理庫(kù)。valtio 讓數(shù)據(jù)管理在 React 和原生 JS (Vanilla) 中變得更加簡(jiǎn)單的一個(gè)庫(kù),它類(lèi)似于 Vue 的數(shù)據(jù)驅(qū)動(dòng)視圖的理念,使用外部狀態(tài)代理去驅(qū)動(dòng) React 視圖來(lái)更新。

一、狀態(tài)管理庫(kù)
dispatch流派(單向數(shù)據(jù)流-中心化管理):redux、zustand、dva等- 響應(yīng)式流派(中心化管理):
mobx、valtio等 - 原子狀態(tài)流派(原子組件化管理):
recoil、jotai等
值得一提的是:Jotai、Zustand、Valtio 這三個(gè)開(kāi)源狀態(tài)管理庫(kù)都是出自一人之手。
zustand 德語(yǔ) “狀態(tài)”,jotai 日語(yǔ) “狀態(tài)”、valtio 芬蘭語(yǔ) “狀態(tài)”。
作者叫做 Daishi Kato,他是日本東京人,是個(gè)全職開(kāi)源作者。
二、Jotai、Zustand、Valtio 使用對(duì)比
- Zustand
import { create } from "zustand";
const useStore = create((set) => ({
count: 0,
inc: () => set((state) => ({ count: state.count + 1 })),
}));
export default function Counter() {
const count = useStore((state) => state.count);
const inc = useStore((state) => state.inc);
return (
<div>
{count}
<button onClick={inc}>+1</button>
</div>
);
}
- Jotai:每個(gè)狀態(tài)都是原子化,用法和原生的 useState 有點(diǎn)像
import { atom, useAtom } from "jotai";
const countAtom = atom(0);
function Counter() {
const [count, setCount] = useAtom(countAtom);
return (
<div>
{count}
<button onClick={() => setCount((v) => v + 1)}>+1</button>
</div>
);
}
- Valtio:和 Vue 的響應(yīng)式類(lèi)似,當(dāng)數(shù)據(jù)發(fā)生變化的時(shí)候就驅(qū)動(dòng)視圖更新
const state = proxy({
dur: 10,
count: 1102
});
const incDur = () => {++state.dur};
const decDur = () => {--state.dur};
const incCount = () => {
++state.count;
setTimeout(incCount, 100 * state.dur);
};
incCount();
export default function Main() {
const snap = useSnapshot(state)
return (
<div>
<h3>{snap.dur}</h3>
<button
disabled={snap.dur <= 1}
onClick={decDur}>
-
</button>
<button
disabled={snap.dur >= 10}
onClick={incDur}>
+
</button>
</div>
);
}
三、Valtio 狀態(tài)管理最佳實(shí)踐
- 創(chuàng)建一個(gè)
store.js文件
import { proxy } from 'valtio'
import { useProxy } from 'valtio/utils'
import { cloneDeep } from 'lodash-es'
export const defaultData = {
activeIndex: 0,
rangeData: [5, 20],
baseSelected: [],
step: {
1: true,
2: false,
3: false,
4: false,
5: false,
6: false,
},
}
const state = cloneDeep(defaultData)
const store = proxy(state)
export const useStore = () => {
return useProxy(store)
}
export function resetData() {
Object.entries(defaultData).forEach(
([key, value]) => {
store[key] = cloneDeep(value)
}
)
}
- 在組件中使用
import { useStore, resetData } from '@/store/store'
const tabs = [
{
value: '1',
label: '基礎(chǔ)設(shè)置',
},
{
value: '2',
label: '高級(jí)設(shè)置',
},
{
value: '3',
label: '其他設(shè)置',
}
]
const list = [
{
value: '1',
label: '標(biāo)簽'
},
{
value: '2',
label: '分類(lèi)'
},
{
value: '3',
label: '作者'
},
]
export default function Main() {
const store = useStore()
useEffect(() => {
return () => {
// 在組件卸載的時(shí)候重置數(shù)據(jù)
resetData()
}
}, []);
function onSelect(id) {
if (store.baseSelected.includes(id)) {
store.baseSelected =
store.baseSelected.filter(
(item) => item !== id
)
} else {
if (store.baseSelected.length >= 5) return
store.baseSelected.push(id)
}
}
return (
<div>
{
tabs.map(({ value, label }, index) => (
<div
key={value}
onClick={() => {
store.activeIndex = index
}}
>
{label}
</div>
))
}
<hr/>
{
list.map(({ value, label }) => (
<div
key={value}
onClick={() => {
onSelect(value)
}}
>
{label}
</div>
))
}
</div>
);
}
useProxy 其實(shí)就是對(duì)取 useSnapshot() 或 store 數(shù)據(jù)的封裝,這個(gè) hook 也很簡(jiǎn)單,就是判斷是渲染期間(渲染體內(nèi))就返回 useSnapshot() 的快照數(shù)據(jù),非渲染期間(非渲染體內(nèi))就返回原始的 store 數(shù)據(jù),和我們自己手寫(xiě)的是差不多的,只不過(guò)這個(gè) hook 幫我們把這個(gè)過(guò)程封裝了起來(lái)。
到此這篇關(guān)于React+valtio響應(yīng)式狀態(tài)管理的文章就介紹到這了,更多相關(guān)React valtio響應(yīng)式狀態(tài)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
比ant更豐富Modal組件功能實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了比ant更豐富Modal組件功能實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
React父組件數(shù)據(jù)實(shí)時(shí)更新了,子組件沒(méi)有更新的問(wèn)題
這篇文章主要介紹了React父組件數(shù)據(jù)實(shí)時(shí)更新了,子組件沒(méi)有更新的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
關(guān)于React Native報(bào)Cannot initialize a parameter of type''NSArra
這篇文章主要介紹了關(guān)于React Native報(bào)Cannot initialize a parameter of type'NSArray<id<RCTBridgeModule>>錯(cuò)誤,本文給大家分享解決方案,需要的朋友可以參考下2021-05-05
react.js使用webpack搭配環(huán)境的入門(mén)教程
本文主要介紹了react 使用webpack搭配環(huán)境的入門(mén)教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-08-08
Reactjs?+?Nodejs?+?Mongodb?實(shí)現(xiàn)文件上傳功能實(shí)例詳解
今天是使用?Reactjs?+?Nodejs?+?Mongodb?實(shí)現(xiàn)文件上傳功能,前端我們使用?Reactjs?+?Axios?來(lái)搭建前端上傳文件應(yīng)用,后端我們使用?Node.js?+?Express?+?Multer?+?Mongodb?來(lái)搭建后端上傳文件處理應(yīng)用,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2022-06-06
React網(wǎng)絡(luò)請(qǐng)求發(fā)起方法詳細(xì)介紹
在編程開(kāi)發(fā)中,網(wǎng)絡(luò)數(shù)據(jù)請(qǐng)求是必不可少的,這篇文章主要介紹了React網(wǎng)絡(luò)請(qǐng)求發(fā)起方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-09-09

