介绍
一个小型、快速且可扩展的 Bearbones 状态管理解决方案。 Zustand 有一个基于 hooks 的舒适 API。它不是样板文件或固执己见,但有足够的惯例来明确和类似通量。 zustand官网
zustand使用方法及调试工具的安装使用
- 安装包
npm install zustand
2.创建store仓库
import {
create } from 'zustand'
const useStore = create((set) => {
return {
count: 0,
//如果用到原数据,可以携程函数式写法,也可以使用对象式写法世界进行赋值
inc: () => {
set(state => ({
count: state.count + 1 }))
},
removeAllBears: () => set({
count: 0 }),
}
})
export default useStore
- 在组件内使用
import useStore from './store/useCounterStore.js'
function App() {
//直接解构出来用即可
const {
count, inc } = useStore()
return <button onClick={
inc}>{
count}