Skip to content

Commit

Permalink
Make it explicit that reading from context directly is only supported…
Browse files Browse the repository at this point in the history
… when not having access to the Fiber tree
  • Loading branch information
Sebastian Silbermann committed Feb 29, 2024
1 parent 601bdf3 commit 583e634
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion packages/react-debug-tools/src/ReactDebugHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,16 @@ function readContext<T>(context: ReactContext<T>): T {
// There's no sensible value to return.
return (null: any);
}
if (currentContextDependency === null) {
if (currentFiber === null) {
// Hook inspection without access to the Fiber tree.
return context._currentValue;
}
if (currentContextDependency === null) {
throw new Error(
'Context reads do not line up with context dependencies. This is a bug in React.',
);
}

// For now we don't expose readContext usage in the hooks debugging info.
const value = ((currentContextDependency.memoizedValue: any): T);
currentContextDependency = currentContextDependency.next;
Expand Down

0 comments on commit 583e634

Please sign in to comment.