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 - Custom equal

Create a store

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

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

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

/* the count hook for function component */
/* the component will NOT be re-rendered if the diff */
/* between current count and next count is less or equal to 1 */
export const useCount = () =>
  useSelect(state => state.count, {
    stateEqualityFn: ({ count: prevCount }, { count: nextCount }) => {
      return Math.abs(prevCount - nextCount) <= 1;
    }
  });

/* increase the counter by 1, this will NOT trigger component re-render */
export const increaseCount = () =>
  dispatch(state => ({ count: state.count + 1 }));

/* increase the counter by 2, this will trigger component re-render */
export const doubleIncreaseCount = () =>
  dispatch(state => ({ count: state.count + 2 }));

Use the store in component

SolidCustomEqual
import {
  useCount,
  increaseCount,
  doubleIncreaseCount
} from "./SolidStoreCustomEqual";
import SolidCounter from "./SolidCounter";

const SolidCustomEqual = () => {
  const count = useCount();
  return (
    <>
      <SolidCounter value={count} onIncrement={increaseCount} />
      <button onClick={doubleIncreaseCount}>+2</button>
    </>
  );
};

export default SolidCustomEqual;