Skip to content

Latest commit

 

History

History
64 lines (43 loc) · 1.01 KB

File metadata and controls

64 lines (43 loc) · 1.01 KB

Usage Guide

Installation

Installation for React

npm install @illostack/react-store

Installation for the Store

npm install @illostack/store

Creating a Store

You can create a store using createStore, which accepts an object defining the initial state.

import { createStore } from "@illostack/store";

const store = createStore({
  count: 0
});

Using in React

useStore Hook

import { useStore } from "@illostack/react-store";

const Counter = () => {
  const count = useStore(store, state => state.count);
  return <div>{count}</div>;
};

useStoreEffect Hook

import { useStoreEffect } from "@illostack/react-store";

const Counter = () => {
  useStoreEffect(store, state => state.count, (state, prevState) => {
    console.log("Count changed", state.count);
  });
  
  // No re-renders
  ...
};

Updating State

You can update the state of the store using the update method.

store.update(state => ({ count: state.count + 1 }));