import {computed, ComputedRef} from "vue"; import {useState} from "./index"; import { Dispatch, Reducer, ReducerAction, ReducerState, ReducerStateWithoutAction, DispatchWithoutAction, ReducerWithoutAction } from "./misc/types"; import {resolveHookState} from './misc/hookState' function useReducer | ReducerWithoutAction)>( reducer: R, initialState: ReducerState, initializer?: (arg: ReducerState) => ReducerState ): [ComputedRef>, Dispatch> | DispatchWithoutAction]; function useReducer | ReducerWithoutAction), I>( reducer: R, initialState: I | ReducerState, initializer?: (arg: I | ReducerState) => ReducerState ): [ComputedRef, Dispatch> | DispatchWithoutAction] { if (initializer) { initialState = initializer(resolveHookState(initialState)); } const [state, setState] = useState(initialState); const dispatch: Dispatch = (action) => { setState(prevState => reducer(prevState, action)) }; return [computed(() => { return state.value; }), dispatch]; }; export default useReducer;