MCPcopy
hub / github.com/facebook/react / useMemo

Function useMemo

packages/react-server/src/ReactFizzHooks.js:443–471  ·  view source on GitHub ↗
(nextCreate: () => T, deps: Array<mixed> | void | null)

Source from the content-addressed store, hash-verified

441}
442
443function useMemo<T>(nextCreate: () => T, deps: Array<mixed> | void | null): T {
444 currentlyRenderingComponent = resolveCurrentlyRenderingComponent();
445 workInProgressHook = createWorkInProgressHook();
446
447 const nextDeps = deps === undefined ? null : deps;
448
449 if (workInProgressHook !== null) {
450 const prevState = workInProgressHook.memoizedState;
451 if (prevState !== null) {
452 if (nextDeps !== null) {
453 const prevDeps = prevState[1];
454 if (areHookInputsEqual(nextDeps, prevDeps)) {
455 return prevState[0];
456 }
457 }
458 }
459 }
460
461 if (__DEV__) {
462 isInHookUserCodeInDev = true;
463 }
464 const nextValue = nextCreate();
465 if (__DEV__) {
466 isInHookUserCodeInDev = false;
467 }
468 // $FlowFixMe[incompatible-use] found when upgrading Flow
469 workInProgressHook.memoizedState = [nextValue, nextDeps];
470 return nextValue;
471}
472
473function useRef<T>(initialValue: T): {current: T} {
474 currentlyRenderingComponent = resolveCurrentlyRenderingComponent();

Callers 1

useCallbackFunction · 0.70

Calls 3

createWorkInProgressHookFunction · 0.85
areHookInputsEqualFunction · 0.85

Tested by

no test coverage detected