MCPcopy
hub / github.com/NervJS/nerv / useReducer

Function useReducer

packages/nerv/src/hooks.ts:86–107  ·  view source on GitHub ↗
(
  reducer: R,
  initialState: I & ReducerState<R>,
  initializer?: (arg: I & ReducerState<R>) => ReducerState<R>
)

Source from the content-addressed store, hash-verified

84}
85
86export 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
109function areDepsChanged (prevDeps?: DependencyList, deps?: DependencyList) {
110 if (isNullOrUndef(prevDeps) || isNullOrUndef(deps)) {

Callers 2

CounterFunction · 0.90
CompFunction · 0.90

Calls 5

isFunctionFunction · 0.90
isUndefinedFunction · 0.90
getHooksFunction · 0.85
reducerFunction · 0.85
setStateMethod · 0.80

Tested by 2

CounterFunction · 0.72
CompFunction · 0.72