MCPcopy Index your code
hub / github.com/react/react / readContextForConsumer

Function readContextForConsumer

packages/react-reconciler/src/ReactFiberNewContext.js:529–571  ·  view source on GitHub ↗
(
  consumer: Fiber | null,
  context: ReactContext<T>,
)

Source from the content-addressed store, hash-verified

527}
528
529function readContextForConsumer<T>(
530 consumer: Fiber | null,
531 context: ReactContext<T>,
532): T {
533 const value = isPrimaryRenderer
534 ? context._currentValue
535 : context._currentValue2;
536
537 const contextItem = {
538 context: ((context: any): ReactContext<mixed>),
539 memoizedValue: value,
540 next: null,
541 };
542
543 if (lastContextDependency === null) {
544 if (consumer === null) {
545 throw new Error(
546 'Context can only be read while React is rendering. ' +
547 'In classes, you can read it in the render method or getDerivedStateFromProps. ' +
548 'In function components, you can read it directly in the function body, but not ' +
549 'inside Hooks like useReducer() or useMemo().',
550 );
551 }
552
553 // This is the first dependency for this component. Create a new list.
554 lastContextDependency = contextItem;
555 consumer.dependencies = __DEV__
556 ? {
557 lanes: NoLanes,
558 firstContext: contextItem,
559 _debugThenableState: null,
560 }
561 : {
562 lanes: NoLanes,
563 firstContext: contextItem,
564 };
565 consumer.flags |= NeedsPropagation;
566 } else {
567 // Append a new context item.
568 lastContextDependency = lastContextDependency.next = contextItem;
569 }
570 return value;
571}

Callers 2

readContextFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected