1.0.14 • Published 2 years ago

gstatem v1.0.14

Weekly downloads
-
License
MIT
Repository
github
Last release
2 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

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

1.0.0-alpha.12

2 years ago

1.0.0-alpha.11

2 years ago

1.0.0-alpha.10

2 years ago

1.0.0-alpha.9

2 years ago

1.0.0-alpha.6

2 years ago

1.0.0-alpha.5

2 years ago

1.0.0-alpha.4

2 years ago

1.0.0-alpha.2

2 years ago