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 solid-gstatem

yarn

yarn add solid-gstatem

Live demo

Solid - Multiple stores

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

Create store 1

SolidMultiStore1
import * as solid from "solid-js";
import { create } from "solid-gstatem";

const initialState = { count1: 0 };
const { useSelect, dispatch } = create(solid, {
  state: initialState
});

/* the count hook for function component */
export const useCount1 = () => useSelect(state => state.count1);

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

Create store 2

SolidMultiStore2
import * as solid from "solid-js";
import { create } from "solid-gstatem";

const initialState = { count2: 0 };
const { useSelect, dispatch } = create(solid, {
  state: initialState
});

/* the count hook for function component */
export const useCount2 = () => useSelect(state => state.count2);

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

Use the store in component

SolidMultiStores
import SolidCounter from "./SolidCounter";
import { useCount1, increaseCount1 } from "./SolidMultiStore1";
import { useCount2, increaseCount2 } from "./SolidMultiStore2";

const SolidMultiStores = () => {
  const count1 = useCount1();
  const count2 = useCount2();

  return (
    <>
      Store1 <SolidCounter value={count1} onIncrement={increaseCount1} />
      Store2 <SolidCounter value={count2} onIncrement={increaseCount2} />
    </>
  );
};

export default SolidMultiStores;