Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DevTools: Fix inspecting components with multiple reads of the same Context in React 17 #28974

Merged
merged 2 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions packages/react-debug-tools/src/ReactDebugHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,20 @@ function readContext<T>(context: ReactContext<T>): T {
);
}

let value: T;
// For now we don't expose readContext usage in the hooks debugging info.
const value = hasOwnProperty.call(currentContextDependency, 'memoizedValue')
? // $FlowFixMe[incompatible-use] Flow thinks `hasOwnProperty` mutates `currentContextDependency`
((currentContextDependency.memoizedValue: any): T)
: // Before React 18, we did not have `memoizedValue` so we rely on `setupContexts` in those versions.
// $FlowFixMe[incompatible-use] Flow thinks `hasOwnProperty` mutates `currentContextDependency`
((currentContextDependency.context._currentValue: any): T);
// $FlowFixMe[incompatible-use] Flow thinks `hasOwnProperty` mutates `currentContextDependency`
currentContextDependency = currentContextDependency.next;
if (hasOwnProperty.call(currentContextDependency, 'memoizedValue')) {
// $FlowFixMe[incompatible-use] Flow thinks `hasOwnProperty` mutates `currentContextDependency`
value = ((currentContextDependency.memoizedValue: any): T);

// $FlowFixMe[incompatible-use] Flow thinks `hasOwnProperty` mutates `currentContextDependency`
currentContextDependency = currentContextDependency.next;
} else {
// Before React 18, we did not have `memoizedValue` so we rely on `setupContexts` in those versions.
// Multiple reads of the same context were also only tracked as a single dependency.
// We just give up on advancing context dependencies and solely rely on `setupContexts`.
value = context._currentValue;
}

return value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,45 @@ describe('ReactHooksInspectionIntegration', () => {
`);
});

// @reactVersion >= 16.8
it('should inspect the value of the current provider in useContext reading the same context multiple times', async () => {
Copy link
Contributor

@hoxyq hoxyq May 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to run this test against React >=17, you should include // @reactVersion >= 17.0 pragma above the test definition

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opted for 16.8 since that's when hooks got introduced.

const ContextA = React.createContext('default A');
const ContextB = React.createContext('default B');
function Foo(props) {
React.useContext(ContextA);
React.useContext(ContextA);
React.useContext(ContextB);
React.useContext(ContextB);
React.useContext(ContextA);
React.useContext(ContextB);
React.useContext(ContextB);
React.useContext(ContextB);
return null;
}
let renderer;
await act(() => {
renderer = ReactTestRenderer.create(
<ContextA.Provider value="contextual A">
<Foo prop="prop" />
</ContextA.Provider>,
{unstable_isConcurrent: true},
);
});
const childFiber = renderer.root.findByType(Foo)._currentFiber();
const tree = ReactDebugTools.inspectHooksOfFiber(childFiber);

expect(normalizeSourceLoc(tree)).toEqual([
expect.objectContaining({value: 'contextual A'}),
expect.objectContaining({value: 'contextual A'}),
expect.objectContaining({value: 'default B'}),
expect.objectContaining({value: 'default B'}),
expect.objectContaining({value: 'contextual A'}),
expect.objectContaining({value: 'default B'}),
expect.objectContaining({value: 'default B'}),
expect.objectContaining({value: 'default B'}),
]);
});

it('should inspect forwardRef', async () => {
const obj = function () {};
const Foo = React.forwardRef(function (props, ref) {
Expand Down