react-hooks-global-state-next
Version:
Simple global state for React with Hooks API without Context API
44 lines (43 loc) • 1.45 kB
TypeScript
import { Reducer, SetStateAction } from 'react';
/**
* 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.
*
* @example
* import { createStore } from 'react-hooks-global-state-next';
*
* const initialState = { count: 0 };
* const reducer = ...;
*
* const store = createStore(reducer, initialState);
* const { useStoreState, dispatch } = store;
*
* const Component = () => {
* const count = useStoreState('count');
* ...
* };
*/
export declare const createStore: <State extends object, Action extends {
type: unknown;
}>(reducer: Reducer<State, Action>, initialState?: State, enhancer?: any) => Store<State, Action>;
declare type Store<State, Action> = {
useStoreState: <StateKey extends keyof State>(stateKey: StateKey) => State[StateKey];
/**
* useGlobalState created by createStore is deprecated.
*
* @deprecated useStoreState instead
*/
useGlobalState: <StateKey extends keyof State>(stateKey: StateKey) => readonly [
State[StateKey],
(u: SetStateAction<State[StateKey]>) => void
];
getState: () => State;
dispatch: (action: Action) => Action;
};
export {};