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 - Silent mode

Rendered view

The + button will NOT trigger component re-render.

Clicked: 0 times

Recipes


Create a store

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

Please use get and set functions with care as they won't trigger component re-render.

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

const initialState = { count: 0 };
const { get, set } = create({ state: initialState });

/* This won't trigger component re-render */
export const getCount = () => get(state => state.count);

/* This won't trigger component re-render */
export const increaseCount = () => set(state => ({ count: state.count + 1 }));

Use the store in component

FCSilentMode
import React, { useReducer } from "react";
import { getCount, increaseCount } from "./FCStoreSilentMode";
import Counter from "./Counter";

const FCSilentMode = () => {
  const rerender = useReducer(x => !x, true)[1];

  return (
    <>
      <button onClick={() => rerender()}>re-render</button>
      <Counter value={getCount()} onIncrement={increaseCount} />
    </>
  );
};

export default FCSilentMode;