MCPcopy Index your code
hub / github.com/QuarkGluonPlasma/react-course-code / useState

Function useState

mini-react/src/mini-react.js:207–239  ·  view source on GitHub ↗
(initialState)

Source from the content-addressed store, hash-verified

205 }
206
207 function useState(initialState) {
208 const currentFiber = wipFiber;
209
210 const oldHook = wipFiber.alternate?.stateHooks[stateHookIndex];
211
212 const stateHook = {
213 state: oldHook ? oldHook.state : initialState,
214 queue: oldHook ? oldHook.queue : [],
215 };
216
217 stateHook.queue.forEach((action) => {
218 stateHook.state = action(stateHook.state);
219 });
220
221 stateHook.queue = [];
222
223 stateHookIndex++;
224 wipFiber.stateHooks.push(stateHook);
225
226 function setState(action) {
227 const isFunction = typeof action === "function";
228
229 stateHook.queue.push(isFunction ? action : () => action);
230
231 wipRoot = {
232 ...currentFiber,
233 alternate: currentFiber,
234 };
235 nextUnitOfWork = wipRoot;
236 }
237
238 return [stateHook.state, setState];
239 }
240
241 function useEffect(callback, deps) {
242 const effectHook = {

Callers 15

useCounterFunction · 0.90
ToggleFunction · 0.90
MyLazyloadFunction · 0.90
useMergeStateFunction · 0.90
AppFunction · 0.90
CalendarFunction · 0.90
CalendarFunction · 0.90
AppFunction · 0.90
useMergeStateFunction · 0.90
AppFunction · 0.90
AaaFunction · 0.90
BbbFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected