| 84 | } |
| 85 | |
| 86 | export function useReducer<R extends Reducer<any, any>, I> ( |
| 87 | reducer: R, |
| 88 | initialState: I & ReducerState<R>, |
| 89 | initializer?: (arg: I & ReducerState<R>) => ReducerState<R> |
| 90 | ): [ReducerState<R>, Dispatch<ReducerAction<R>>] { |
| 91 | if (isFunction(initialState)) { |
| 92 | initialState = initialState() |
| 93 | } |
| 94 | const hook = getHooks(Current.index++) as HookReducer<R, I> |
| 95 | if (!hook.state) { |
| 96 | hook.component = Current.current! |
| 97 | hook.state = [ |
| 98 | isUndefined(initializer) ? initialState : initializer(initialState), |
| 99 | (action) => { |
| 100 | hook.state[0] = reducer(hook.state[0], action) |
| 101 | hook.component._disable = false |
| 102 | hook.component.setState({}) |
| 103 | } |
| 104 | ] |
| 105 | } |
| 106 | return hook.state |
| 107 | } |
| 108 | |
| 109 | function areDepsChanged (prevDeps?: DependencyList, deps?: DependencyList) { |
| 110 | if (isNullOrUndef(prevDeps) || isNullOrUndef(deps)) { |