zustand 模版代码

21 阅读1分钟

安装 npm i zustand 创建src/store/index.j文件

import { create } from "zustand"

export const useStore = create((set) => ({
  count: 0,
  increment: () => set((state) => ({ count: state.count + 1 })),
}))

在需要用的地方

import {useStore} from "@/store"
const Home = () => {
    const {count,  increment:inc} = useStore(state=>state)
  return (
    <div>
      <h1>{count}</h1> <button onClick={inc}>+</button>
    </div>
  );
};
export default Home;