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

Function readContext

packages/react-debug-tools/src/ReactDebugHooks.js:161–190  ·  view source on GitHub ↗
(context: ReactContext<T>)

Source from the content-addressed store, hash-verified

159}
160
161function readContext<T>(context: ReactContext<T>): T {
162 if (currentFiber === null) {
163 // Hook inspection without access to the Fiber tree
164 // e.g. when warming up the primitive stack cache or during `ReactDebugTools.inspectHooks()`.
165 return context._currentValue;
166 } else {
167 if (currentContextDependency === null) {
168 throw new Error(
169 'Context reads do not line up with context dependencies. This is a bug in React Debug Tools.',
170 );
171 }
172
173 let value: T;
174 // For now we don't expose readContext usage in the hooks debugging info.
175 if (hasOwnProperty.call(currentContextDependency, 'memoizedValue')) {
176 // $FlowFixMe[incompatible-use] Flow thinks `hasOwnProperty` mutates `currentContextDependency`
177 value = ((currentContextDependency.memoizedValue: any): T);
178
179 // $FlowFixMe[incompatible-use] Flow thinks `hasOwnProperty` mutates `currentContextDependency`
180 currentContextDependency = currentContextDependency.next;
181 } else {
182 // Before React 18, we did not have `memoizedValue` so we rely on `setupContexts` in those versions.
183 // Multiple reads of the same context were also only tracked as a single dependency.
184 // We just give up on advancing context dependencies and solely rely on `setupContexts`.
185 value = context._currentValue;
186 }
187
188 return value;
189 }
190}
191
192const SuspenseException: mixed = new Error(
193 "Suspense Exception: This is not a real error! It's an implementation " +

Callers 5

useFunction · 0.70
useContextFunction · 0.70
useHostTransitionStatusFunction · 0.70
readFunction · 0.50
preloadFunction · 0.50

Calls

no outgoing calls

Tested by

no test coverage detected