zustand-store
TypeScript icon, indicating that this package has built-in type declarations

0.0.12-beta.2 • Public • Published

zustand-store

NPM JavaScript Style Guide

A zustand store with immer, which makes zustand support class pattern.

Install

npm install --save zustand-store

Usage

import React from 'react';
import { useStore } from './store';

const App = () => {
  const store = useStore();
  const onClick = () => {
    store.updateTime(Date.now());
  };
  const isLoading = store.loading.updateTime;

  return (
    <div>
      <div>current time: {isLoading ? 'loading...' : store.time} 😄"</div>
      <button onClick={onClick}>click me</button>
    </div>
  );
};

export default App;

./store.tsx

import Store from 'zustand-store';

class StoreClass extends Store.BaseStore<StoreClass> {
  public time = Date.now();

  @Store.loading()
  public async updateTime(time: number) {
    return new Promise<void>((resolve) => {
      setTimeout(() => {
        this.set((state) => {
          state.time = time;
        });
        resolve();
      }, 2000);
    });
  }
}
export const useStore = Store.create(StoreClass);

License

MIT © JJVvV

Readme

Keywords

none

Package Sidebar

Install

npm i zustand-store

Weekly Downloads

0

Version

0.0.12-beta.2

License

MIT

Unpacked Size

24.3 kB

Total Files

10

Last publish

Collaborators

  • jjvvv