import { useCallback, useRef } from '../lib/teact/teact'; import useForceUpdate from './useForceUpdate'; export type ReducerAction = { type: Actions; payload?: any }; export type StateReducer = (state: State, action: ReducerAction) => State; export type Dispatch = (action: ReducerAction) => State; export default function useReducer( reducer: StateReducer, initialState: State, ) { const forceUpdate = useForceUpdate(); const reducerRef = useRef(reducer); const state = useRef(initialState); const dispatch = useCallback((action: ReducerAction) => { state.current = reducerRef.current(state.current, action); forceUpdate(); return state.current; }, []); return [ state.current, dispatch, ] as [State, Dispatch]; }