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

0.1.2 • Public • Published

tiny-global-store npm bundle size

Tiny state management library for you React application.

Install

npm install tiny-global-store

# or

yarn add tiny-global-store

# or

pnpm add tiny-global-store

Usage

First you need to create a store with an initial value and a hook with all actions available for the store:

import { createStore, createHook } from "tiny-global-store";

interface Store {
    count: number;
    message: string;
}

const store = createStore<Store>({ count: 0, message: "Hello world!" });

const useCounterStore = createHook(store, {
    increase: store => ({ ...store, count: store.count + 1 }),
    reset: store => ({ ...store, count: 0 }),
});

Then you can use your store in any component:

function Counter() {
    const { state } = useCounterStore();
    return <h1>Current value is {state.count}</h1>;
}

function Controls() {
    const { actions } = useCounterStore();
    return (
        <>
            <button onClick={actions.increase}>Increase</button>
            <button onClick={actions.reset}>Reset</button>
        </>
    );
}

Package Sidebar

Install

npm i tiny-global-store

Weekly Downloads

0

Version

0.1.2

License

MIT

Unpacked Size

16.2 kB

Total Files

9

Last publish

Collaborators

  • pajecawav