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

Function useState

packages/nerv/src/hooks.ts:67–84  ·  view source on GitHub ↗
(initialState: S | (() => S))

Source from the content-addressed store, hash-verified

65export type Hook = HookEffect & HookState<unknown> & HookReducer<any, unknown> & HookRef<unknown> & HookCallback<any> & HookContext
66
67export function useState<S> (initialState: S | (() => S)): [S, Dispatch<SetStateAction<S>>] {
68 if (isFunction(initialState)) {
69 initialState = initialState()
70 }
71 const hook = getHooks(Current.index++) as HookState<S>
72 if (!hook.state) {
73 hook.component = Current.current!
74 hook.state = [
75 initialState,
76 (action) => {
77 hook.state[0] = isFunction(action) ? action(hook.state[0]) : action
78 hook.component._disable = false
79 hook.component.setState({})
80 }
81 ]
82 }
83 return hook.state
84}
85
86export function useReducer<R extends Reducer<any, any>, I> (
87 reducer: R,

Callers 7

CompFunction · 0.90
hooks.spec.jsFile · 0.90
StateContainerFunction · 0.90
AppFunction · 0.90
CounterFunction · 0.90
ParentFunction · 0.90
ChildFunction · 0.90

Calls 3

isFunctionFunction · 0.90
getHooksFunction · 0.85
setStateMethod · 0.80

Tested by 6

CompFunction · 0.72
StateContainerFunction · 0.72
AppFunction · 0.72
CounterFunction · 0.72
ParentFunction · 0.72
ChildFunction · 0.72