zustand 切片模式使用,persist 中间件持久化状态

zustand - npm

安装 npm i zustand

创建切片目录:

创建切片 channelStore.js

复制代码
import { getChannelsAPI } from "@/apis/article";
const channelStore = (set) => {
    return {
        channelList: [],
        fetchChannelList: async () => {
            const res = await getChannelsAPI()
            set({
                channelList: res.data.data.channels
            })
        }
    }
}
export default channelStore

创建切片numStore.js

复制代码
const numStore = (set) => {
    return {
        num: 0,
        incre: ()=>{
            set(state => ({
                num: state.num+1
            }))
        }
    }
}
export default numStore
创建主Store 如 useStoree 并组合切片
复制代码
import { create } from "zustand";
import channelStore from "./module/channelStore";
import numStore from "./module/numStore";

const useStoree = create((...args) =>{
    return {
        ...channelStore(...args),
        ...numStore(...args)
    }
})
export default useStoree

或者

复制代码
import { create } from 'zustand';
import channelStore  from './module/channelStore';
import numStore  from './module/numStore';

const useStoree = create(set => ({
  ...channelStore(set),
  ...numStore(set)
}))

export default useStoree

在组件中使用useStoree

复制代码
import useStoree from "@/zustandStore";
import { useEffect } from "react";

const ZustanInd = ()=>{
    // const num = useStoree((state) => state.num)
    // const fetchChannelList = useStoree((state) => state.fetchChannelList)
    // const channelList = useStoree((state) =>state.channelList)
    // const incre = useStoree((state) => state.incre)

    const {num,incre,fetchChannelList,channelList} = useStoree()
    
    useEffect(()=>{
        fetchChannelList()
    },[])
    console.log(channelList)

    return<>
    <button onClick={incre}>{num}</button>
    <ul>
        {channelList.map(item=><li key={item.id}>{item.name}</li>)}
    </ul>
    </>
}
export default ZustanInd

使用 persist 中间件 持久化状态

npm install zustand/persist

复制代码
import { create } from 'zustand';
import channelStore  from './module/channelStore';
import numStore  from './module/numStore';
import { persist } from 'zustand/middleware';
const useStoree = create(
    persist(
        set => ({
            ...channelStore(set),
            ...numStore(set)
          }),
          {
            name: 'my-store', // 存储的名字
            getStorage: () => localStorage, // 存储的方式
          }
    )
)

export default useStoree

持久化部分:

复制代码
import { create } from 'zustand';
import channelStore  from './module/channelStore';
import numStore  from './module/numStore';
import { persist } from 'zustand/middleware';
const useStoree = create(
    persist(
        (set) => ({
            ...channelStore(set),
            ...numStore(set)
          }),
          {
            name: 'my-store', // 存储的名字
            getStorage: () => localStorage, // 存储的方式 ,默认是 localStorage,也可设置 sessionStorage
            partialize: (state) => ({ num: state.num }), // 持久化部分状态
          }
    )
)

export default useStoree
相关推荐
然我2 小时前
React 开发通关指南:用 HTML 的思维写 JS🚀🚀
前端·react.js·html
马特说5 小时前
React金融数据分析应用性能优化实战:借助AI辅助解决18万数据量栈溢出Bug
react.js·金融·数据分析
归于尽5 小时前
useEffect玩转React Hooks生命周期
前端·react.js
G等你下课5 小时前
React useEffect 详解与运用
前端·react.js
中微子5 小时前
React Props 传值规范详解
前端·react.js
卸任5 小时前
性能优化大作战:React.memo 在可编辑列表中的奇效
前端·javascript·react.js
LeeAt6 小时前
React Hooks 编程:useState和useEffect的详解
前端·react.js
Dream耀6 小时前
React Hooks 指南:useState 与 useEffect 的用法与技巧
前端·javascript·react.js
我想说一句6 小时前
React Hooks 生存指南:让你的函数组件"活"起来 🧬
前端·javascript·react.js
LeeAt6 小时前
AI单词拍照识别移动端项目(一)
前端·react.js·ai编程