import {Dispatch, useReducer} from 'react'; import {createContext} from 'use-context-selector'; export type TableSearchAction = {type: 'SEARCH', payload: S} | {type: 'RESET'}; type InitStateType = Readonly>; export function useTableSearchContextReducer(init: S) { function initState() { return {...init}; } function reducer(state: S, action: TableSearchAction): S { const {type} = action; switch (type) { case 'SEARCH': return action.payload; case 'RESET': return initState(); default: return state; } } return useReducer(reducer, initState()); } export type TableSearchContext = [S, Dispatch>]; export function createTableSearchContext(state: S) { return createContext>([ {...state}, () => null, ]); }