1.0.14 • Published 3 years ago

gstatem v1.0.14

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

GStatem is a small, simple and fast state-management tool.

Installation

npm

npm i gstatem

yarn

yarn add gstatem

Demos

Usage

Create a store

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

// Store.js
import GStatem, { Subscriber, SetOptions } from "gstatem";

const { get, set, select } = new GStatem({
  /* initial state */
  state: { count: 0 }
});

export const countSelector = state => state.count;

export const getCount = get(countSelector);

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

/* update the store with callback */
export const increaseCount = () =>
  set(state => ({ count: state.count + 1 }), options);

Use without react

import {
  increaseCount,
  resetCount,
  getCount,
  selectCount
} from "../lib/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();
};
1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

1.0.0-alpha.12

3 years ago

1.0.0-alpha.11

3 years ago

1.0.0-alpha.10

3 years ago

1.0.0-alpha.9

3 years ago

1.0.0-alpha.6

3 years ago

1.0.0-alpha.5

3 years ago

1.0.0-alpha.4

3 years ago

1.0.0-alpha.2

3 years ago