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

0.0.25 • Public • Published

简体中文 | English

nostore

Global state management based on React Hooks

GitHub codecov npm version npm downloads npm bundle size (minified) Build Status React

Features

  • One API, Simple but efficient
  • Strongly typed with Typescript
  • React hooks style
  • Minimum granularity update component

It can be used as useState in the global context. Indeed affect the whole body!

Install

$ yarn add nostore
# or
$ npm install nostore --save

Try It Online

Edit react

use

create a store

// store.js

import { createStore } from "nostore";

const useStore = createStore({ count: 1 });

export default useStore;

// action 
export function useDecrease() {
  const [, setStore] = useStore();
  return () => {
    setStore(prevStore => ({
      count: prevStore.count - 1
    }));
  };
}

// multiple actions
export function useAction() {
  const [store, setStore] = useStore();
  return {
    decrease() {
      setStore({
        count: store.count - 1
      });
    },
    // async action
    async increase() {
      await wait(2000);
      setStore(prevStore => ({
        count: prevStore.count + 1
      }));
    }
  };
}	

use store

// Increase.jsx

import useStore, { useAction } from "./store.js";

function Increase() {
  const [store] = useStore();
  const { increase } = useAction();
  return (
    <>
      <h1>{store.count}</h1>
      <button onClick={increase}>increase</button>
    </>
  );
}
// Decrease.jsx

import useStore, { useDecrease } from "./store.js";

function Decrease() {
  const [store] = useStore();
  const decrease = useDecrease();
  return (
    <>
      <h1>{store.count}</h1>
      <button onClick={decrease} />
    </>
  );
}

Package Sidebar

Install

npm i nostore

Weekly Downloads

0

Version

0.0.25

License

MIT

Unpacked Size

20.2 kB

Total Files

12

Last publish

Collaborators

  • titian