Detalhes do pacote

react-hooks-global-state

dai-shi72.4kMIT2.1.0

Simple global state for React with Hooks API without Context API

react, state, hooks, stateless

readme (leia-me)

react-hooks-global-state

CI npm size discord

Simple global state for React with Hooks API without Context API

Introduction

This is a library to provide a global state with React Hooks. It has following characteristics.

  • Optimization for shallow state getter and setter.
    • The library cares the state object only one-level deep.
  • TypeScript type definitions
    • A creator function creates hooks with types inferred.
  • Redux middleware support to some extent
    • Some of libraries in Redux ecosystem can be used.

Install

npm install react-hooks-global-state

Usage

setState style

import React from 'react';
import { createGlobalState } from 'react-hooks-global-state';

const initialState = { count: 0 };
const { useGlobalState } = createGlobalState(initialState);

const Counter = () => {
  const [count, setCount] = useGlobalState('count');
  return (
    <div>
      <span>Counter: {count}</span>
      {/* update state by passing callback function */}
      <button onClick={() => setCount(v => v + 1)}>+1</button>
      {/* update state by passing new value */}
      <button onClick={() => setCount(count - 1)}>-1</button>
    </div>
  );
};

const App = () => (
  <>
    <Counter />
    <Counter />
  </>
);

reducer style

import React from 'react';
import { createStore } from 'react-hooks-global-state';

const reducer = (state, action) => {
  switch (action.type) {
    case 'increment': return { ...state, count: state.count + 1 };
    case 'decrement': return { ...state, count: state.count - 1 };
    default: return state;
  }
};
const initialState = { count: 0 };
const { dispatch, useStoreState } = createStore(reducer, initialState);

const Counter = () => {
  const value = useStoreState('count');
  return (
    <div>
      <span>Counter: {value}</span>
      <button onClick={() => dispatch({ type: 'increment' })}>+1</button>
      <button onClick={() => dispatch({ type: 'decrement' })}>-1</button>
    </div>
  );
};

const App = () => (
  <>
    <Counter />
    <Counter />
  </>
);

API

createGlobalState

Create a global state.

It returns a set of functions

  • useGlobalState: a custom hook works like React.useState
  • getGlobalState: a function to get a global state by key outside React
  • setGlobalState: a function to set a global state by key outside React
  • subscribe: a function that subscribes to state changes

Parameters

  • initialState State

Examples

import { createGlobalState } from 'react-hooks-global-state';

const { useGlobalState } = createGlobalState({ count: 0 });

const Component = () => {
  const [count, setCount] = useGlobalState('count');
  ...
};

createStore

Create a global store.

It returns a set of functions

  • useStoreState: a custom hook to read store state by key
  • getState: a function to get store state by key outside React
  • dispatch: a function to dispatch an action to store

A store works somewhat similarly to Redux, but not the same.

Parameters

  • reducer Reducer\<State, Action>
  • initialState State (optional, default (reducer as any)(undefined,{type:undefined}))
  • enhancer any?

Examples

import { createStore } from 'react-hooks-global-state';

const initialState = { count: 0 };
const reducer = ...;

const store = createStore(reducer, initialState);
const { useStoreState, dispatch } = store;

const Component = () => {
  const count = useStoreState('count');
  ...
};

Returns Store\<State, Action>

useGlobalState

useGlobalState created by createStore is deprecated.

Type: function (stateKey: StateKey): any

Meta

  • deprecated: useStoreState instead

Examples

The examples folder contains working examples. You can run one of them with

PORT=8080 npm run examples:01_minimal

and open http://localhost:8080 in your web browser.

You can also try them in codesandbox.io: 01 02 03 04 05 06 07 08 09 10 11 13

Blogs

Community Wiki

changelog (log de mudanças)

Change Log

[Unreleased]

[2.1.0] - 2022-12-04

Added

  • expose "subscribe" function for "createGlobalState" #85

[2.0.0] - 2022-08-05

Changed from 2.0.0-rc.0

  • Depends on zustand v4.0.0

[2.0.0-rc.0] - 2022-05-28

Changed

  • Re-implemented with zustand as a dependency
  • createStore returns useStoreState
    • useGlobalState returned by createStore is deprecated

      Removed

  • BREAKING CHANGE: drop reduxDevToolsExt

[1.0.2] - 2021-08-14

Changed

  • Fix package.json properly for ESM

[1.0.1] - 2020-07-04

Changed

  • Modern build

[1.0.0] - 2020-02-18

Added

  • More tests

    Changed

  • Improve naming: stateKey

[1.0.0-alpha.2] - 2020-01-25

Changed

  • Migration to TypeScript (#36)
    • BREAKING CHANGE: reduxDevToolsExt is now in index

[1.0.0-alpha.1] - 2020-01-24

Changed

  • New API without context (#35)
    • BREAKING CHANGE: No longer GlobalStateProvider required

[0.17.0] - 2020-01-09

Changed

  • Support state branching (#31)

[0.16.0] - 2019-10-22

Changed

  • Improve typings with React types

[0.15.0] - 2019-09-25

Changed

  • Remove not-in-render warning in getGlobalState (#26)

[0.14.0] - 2019-06-15

Changed

  • Better error message for missing GlobalStateProvider

[0.13.0] - 2019-06-11

Added

  • Check name availability in runtime (DEV only)

    Changed

  • Update devDependencies

[0.12.0] - 2019-06-07

Changed

  • Proper GlobalStateProvider type

    Added

  • Add getGlobalState function

[0.11.0] - 2019-05-25

Changed

  • Fix failing to update state during initialization

[0.10.0] - 2019-05-25

Changed

  • Trick to support react-hot-loader

[0.9.0] - 2019-03-18

Changed

  • Better typings for optional initialState

[0.8.0] - 2019-03-16

Changed

  • Experimental support for eliminating initialState
  • Copied devtools.d.ts into the dist folder

[0.7.0] - 2019-02-11

Changed

  • Fixed type definition of CreateGlobalState
  • Eliminated React warning for using unstable_observedBits

[0.6.0] - 2019-02-07

Changed

  • Updated Dependencies (react 16.8)

[0.5.0] - 2019-01-26

Changed

  • AnyEnhancer type hack for Redux

[0.4.0] - 2018-12-03

Changed

  • Fix Initialization bug (#2)

[0.3.0] - 2018-11-12

Changed

  • New API using Context and observedBits

[0.2.0] - 2018-11-09

Changed

  • Hacky/dirty Redux DevTools Extension support
  • Fix type definition

[0.1.0] - 2018-11-07

Changed

  • API changed to useGlobalState/setGlobalState

[0.0.5] - 2018-11-06

Changed

  • Store enhancer support

[0.0.4] - 2018-11-05

Changed

  • Redux-like createStore
  • Update README

[0.0.3] - 2018-11-04

Added

  • Reducer support

[0.0.2] - 2018-10-30

Changed

  • Update README

[0.0.1] - 2018-10-28

Added

  • Initial experimental release