MCPcopy Create free account
hub / github.com/reactjs/react-rails / readContext

Function readContext

lib/assets/react-source/development/react.js:15779–15826  ·  view source on GitHub ↗
(context, observedBits)

Source from the content-addressed store, hash-verified

15777 }
15778 }
15779 function readContext(context, observedBits) {
15780 {
15781 // This warning would fire if you read context inside a Hook like useMemo.
15782 // Unlike the class check below, it's not enforced in production for perf.
15783 if (isDisallowedContextReadInDEV) {
15784 error('Context can only be read while React is rendering. ' + 'In classes, you can read it in the render method or getDerivedStateFromProps. ' + 'In function components, you can read it directly in the function body, but not ' + 'inside Hooks like useReducer() or useMemo().');
15785 }
15786 }
15787
15788 if (lastContextWithAllBitsObserved === context) ; else if (observedBits === false || observedBits === 0) ; else {
15789 var resolvedObservedBits; // Avoid deopting on observable arguments or heterogeneous types.
15790
15791 if (typeof observedBits !== 'number' || observedBits === MAX_SIGNED_31_BIT_INT) {
15792 // Observe all updates.
15793 lastContextWithAllBitsObserved = context;
15794 resolvedObservedBits = MAX_SIGNED_31_BIT_INT;
15795 } else {
15796 resolvedObservedBits = observedBits;
15797 }
15798
15799 var contextItem = {
15800 context: context,
15801 observedBits: resolvedObservedBits,
15802 next: null
15803 };
15804
15805 if (lastContextDependency === null) {
15806 if (!(currentlyRenderingFiber !== null)) {
15807 {
15808 throw Error( "Context can only be read while React is rendering. In classes, you can read it in the render method or getDerivedStateFromProps. In function components, you can read it directly in the function body, but not inside Hooks like useReducer() or useMemo()." );
15809 }
15810 } // This is the first dependency for this component. Create a new list.
15811
15812
15813 lastContextDependency = contextItem;
15814 currentlyRenderingFiber.dependencies = {
15815 expirationTime: NoWork,
15816 firstContext: contextItem,
15817 responders: null
15818 };
15819 } else {
15820 // Append a new context item.
15821 lastContextDependency = lastContextDependency.next = contextItem;
15822 }
15823 }
15824
15825 return context._currentValue ;
15826 }
15827
15828 var UpdateState = 0;
15829 var ReplaceState = 1;

Callers 6

constructClassInstanceFunction · 0.70
mountClassInstanceFunction · 0.70
resumeMountClassInstanceFunction · 0.70
updateClassInstanceFunction · 0.70
react.jsFile · 0.70
updateContextConsumerFunction · 0.70

Calls 1

errorFunction · 0.70

Tested by

no test coverage detected