No Preview

Sorry, but you either have no stories or none are selected somehow.

If the problem persists, check the browser console, or the terminal you've run Storybook from.

Installation

npm

npm i gstatem

yarn

yarn add gstatem

Vanilla - basic usage

Create a store

The exported functions can be used anywhere - in component, utils file, event listener, setTimeout, setInterval and promise callbacks.

VanillaStore
import GStatem from "gstatem";

const initialState = { count: 0 };
const { get, set, select, dispatch } = new GStatem({
  state: initialState
});

export const countSelector = state => state.count;

export const getCount = get(countSelector);

/* select count for non function component */
export const selectCount = subscribe => select(countSelector, subscribe);

/* increase the counter */
export const increaseCount = () =>
  dispatch(state => ({ count: state.count + 1 }));

/* reset counter */
export const resetCount = options => set(initialState, options);

Use the store in pure javascript

VanillaBasicUsage
import {
  increaseCount,
  resetCount,
  getCount,
  selectCount
} from "./VanillaStore";

/* select count */
const [count, unsubCount] = selectCount(state =>
  console.log("updated count", state.count)
);
console.log(count); // count is 0

increaseCount();
console.log(getCount()); // count is 1

/* reset count */
resetCount();
console.log(getCount()); // count is 0

window.onbeforeunload = () => {
  /* unsubscribe count on page unload */
  unsubCount();
};