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

yarn

yarn add react-gstatem

Function component - Basic usage

Rendered view

Clicked: 0 times

Recipes


Create a store

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

FCStore
import { create } from "react-gstatem";

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

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

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

/* reset counter */
export const resetCount = () => dispatch(initialState);

Use the store in component

FCBasicUsage
import React from "react";
import Counter from "./Counter";
import { increaseCount, resetCount, useCount } from "./FCStore";

const FCBasicUsage = () => {
  const count = useCount();
  return (
    <Counter value={count} onIncrement={increaseCount} onReset={resetCount} />
  );
};

export default FCBasicUsage;