当前位置: 首页> 娱乐> 影视 > 【zustand】快速使用

【zustand】快速使用

时间:2025/7/14 18:10:43来源:https://blog.csdn.net/XiugongHao/article/details/139270456 浏览次数:0次

基本使用

const useStore = create((set) => {return {// 状态数据count: 0,// 修改状态数据的方法inc: () => {set((state) => ({count: state.count + 1})) // 最后这个 ()  的意思是返回了一个对象,基于原数据进行计算得到// set({count: 100})  // 不需要使用到 老数据}}
})function App() {const {count, inc} = useStore()return (<><button onClick={inc}>{count}</button></>)
}

异步

const useStore = create((set) => {return {// 状态数据channelList: [],// 异步方法fetchChannelList: async () => {const res = await fetch(URL)const data = await res.json()set({channelList: data.data.channels})}}
})

切片

const createCounterStore = (set) => {return {count: 0,inc: () => set((state) => ({count: state.count + 1}))}
}
const createChannelStore = (set) => {return {// 状态数据channelList: [],// 异步方法fetchChannelList: async () => {const res = await fetch(URL)const data = await res.json()set({channelList: data.data.channels})}} 
}
const useStore = create((...a) => ({...createCounterStore(...a),...createChannelStore(...a)
}))
关键字:【zustand】快速使用

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: