timmydoza / global-hook

A global version of the useState React hook with a simple API.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

global-hook

Build Status

A global version of React's useState hook.

See a live example here: https://codesandbox.io/s/global-hook-example-jhdn6

Usage:

import useGlobalState from 'global-hook';
const initialState = 0;

function Counter() {
  const [count, setCount] = useGlobalState(initialState, 'uniqueNamespace');
  return (
    <div>
      <span>{count}</span>
      <button onClick={() => setCount(count + 1)}>+</button>
      <button onClick={() => setCount(count - 1)}>-</button>
    </div>
  );
}

If you prefer to create your own instance, use createUseGlobalState:

import { createUseGlobalState } from 'global-hook';
export const useGlobalState = createUseGlobalState();

About

A global version of the useState React hook with a simple API.

License:MIT License


Languages

Language:TypeScript 98.7%Language:JavaScript 1.3%