UNPKG

react-named-reducer

Version:

React Component to easily manage State through reducers using hooks. with typings for Typescript and Flow

37 lines (27 loc) 1.1 kB
// Copyright (c) 2019 Gonzalo Müller Bravo. import type { Context, Element, } from 'react' export interface Dispatcher<ACTION> { (value: ACTION): void; } export interface Reducer<STATE, ACTION> { (prevState: STATE, action: ACTION): STATE; } export type NamedReducerValue<STATE, ACTION> = [STATE, Dispatcher<ACTION>] export interface NamedReducerProps<STATE, ACTION> { name: string; reducer: Reducer<STATE, ACTION>; initialState: STATE; children: Element<any>; } export interface NamedReducerInterface<STATE, ACTION> { state: STATE; dispatch: Dispatcher<ACTION>; } declare export function NamedReducer<STATE, ACTION>(props: NamedReducerProps<STATE, ACTION>): Element<typeof NamedReducer> declare export function useNamedReducer<STATE, ACTION>(name: string): NamedReducerInterface<STATE, ACTION> declare export function useReducerState<STATE>(name: string): STATE declare export function useReducerDispatcher<ACTION>(name: string): Dispatcher<ACTION> declare export function useNamedReducerContext<STATE, ACTION>(name: string): Context<NamedReducerValue<STATE, ACTION>>