MCPcopy
hub / github.com/preactjs/preact / getHookState

Function getHookState

hooks/src/index.js:139–162  ·  view source on GitHub ↗

* Get a hook's state from the currentComponent * @param {number} index The index of the hook to get * @param {number} type The index of the hook to get * @returns {any}

(index, type)

Source from the content-addressed store, hash-verified

137 * @returns {any}
138 */
139function getHookState(index, type) {
140 if (options._hook) {
141 options._hook(currentComponent, index, currentHook || type);
142 }
143 currentHook = 0;
144
145 // Largely inspired by:
146 // * https://github.com/michael-klein/funcy.js/blob/f6be73468e6ec46b0ff5aa3cc4c9baf72a29025a/src/hooks/core_hooks.mjs
147 // * https://github.com/michael-klein/funcy.js/blob/650beaa58c43c33a74820a3c98b3c7079cf2e333/src/renderer.mjs
148 // Other implementations to look at:
149 // * https://codesandbox.io/s/mnox05qp8
150 const hooks =
151 currentComponent.__hooks ||
152 (currentComponent.__hooks = {
153 _list: [],
154 _pendingEffects: []
155 });
156
157 if (index >= hooks._list.length) {
158 hooks._list.push({});
159 }
160
161 return hooks._list[index];
162}
163
164/**
165 * @template {unknown} S

Callers 7

useReducerFunction · 0.85
useEffectFunction · 0.85
useLayoutEffectFunction · 0.85
useMemoFunction · 0.85
useContextFunction · 0.85
useErrorBoundaryFunction · 0.85
useIdFunction · 0.85

Calls 1

_hookMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…