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] Support FunctionComponent.contextTypes #19028

Closed
wants to merge 18 commits into from

Conversation

bl00mber
Copy link
Contributor

resolve #15039
Since stateless components (FunctionComponent) have no state, legacyContext cannot be extracted from them. The proposed solution is to traverse fiber tree upwards until stateful component is met and extract legacyContext from it.

@codesandbox-ci
Copy link

codesandbox-ci bot commented May 28, 2020

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 745204b:

Sandbox Source
React Configuration

@bl00mber bl00mber changed the title DevTools: Support FunctionComponent.contextTypes [DevTools] Support FunctionComponent.contextTypes May 28, 2020
@sizebot
Copy link

sizebot commented May 28, 2020

Details of bundled changes.

Comparing: 38a512a...745204b

react-debug-tools

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-debug-tools.development.js +4.7% +2.9% 20.5 KB 21.47 KB 5.67 KB 5.83 KB NODE_DEV
react-debug-tools.production.min.js 🔺+5.3% 🔺+5.1% 5.48 KB 5.77 KB 2.13 KB 2.23 KB NODE_PROD

Size changes (experimental)

Generated by 🚫 dangerJS against 745204b

@sizebot
Copy link

sizebot commented May 28, 2020

Details of bundled changes.

Comparing: 38a512a...745204b

react-debug-tools

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-debug-tools.development.js +4.7% +2.8% 20.49 KB 21.45 KB 5.66 KB 5.83 KB NODE_DEV
react-debug-tools.production.min.js 🔺+5.3% 🔺+5.0% 5.47 KB 5.76 KB 2.12 KB 2.22 KB NODE_PROD

Size changes (stable)

Generated by 🚫 dangerJS against 745204b

@bl00mber bl00mber force-pushed the devtools/support-contextTypes branch from 7652312 to f630986 Compare May 28, 2020 00:57
@bl00mber bl00mber force-pushed the devtools/support-contextTypes branch from f630986 to 33a86ec Compare May 28, 2020 01:06
@sebmarkbage
Copy link
Collaborator

This argument is about to be removed from React and replace with other things so not sure it's worth supporting at this point. DevTools would need to fork to try to figure out if it should include it or not.

@bl00mber
Copy link
Contributor Author

bl00mber commented May 28, 2020

Maybe add version flags and disable this on renderers > v17 then?

@bl00mber bl00mber force-pushed the devtools/support-contextTypes branch 4 times, most recently from b19a42c to 36d78b1 Compare May 28, 2020 11:25
@bl00mber bl00mber force-pushed the devtools/support-contextTypes branch from 36d78b1 to bede31a Compare May 28, 2020 11:29
@sebmarkbage
Copy link
Collaborator

Another case is that forwardRef passes a second arg. That kind of doesn't work already but that's probably fine since it needs to be resilient to missing refs anyway. However, it shouldn't pass context to a forwardRef component even if it has contextTypes.

(We also have an experimental feature, Blocks, that provides a second arg but I'll remove that one.)

@bl00mber
Copy link
Contributor Author

Disabled for all types except FunctionComponent.

@bl00mber bl00mber force-pushed the devtools/support-contextTypes branch from 8aec60d to 882d0df Compare May 29, 2020 12:18
@bl00mber bl00mber changed the title [DevTools] Support FunctionComponent.contextTypes [DevTools] Support FunctionComponent.contextTypes & fix babel/babel#11216 Jun 1, 2020
@bl00mber bl00mber changed the title [DevTools] Support FunctionComponent.contextTypes & fix babel/babel#11216 [DevTools] Support FunctionComponent.contextTypes & fix babel error Jun 1, 2020
@gaearon
Copy link
Collaborator

gaearon commented Jun 30, 2020

Seems like it doesn't merge cleanly?

@bl00mber
Copy link
Contributor Author

Updated

Copy link
Contributor

@bvaughn bvaughn left a comment

Choose a reason for hiding this comment

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

I'm concerned by the coupling between the react-debug-tools package and a legacy React feature that only DevTools should support going forward. (This PR does not introduce that coupling. It just highlights it.)

A change like this should probably have several new unit tests as well (for both react-debug-tools and React DevTools). You down to include those with this PR?

packages/react-devtools-shared/src/backend/renderer.js Outdated Show resolved Hide resolved
packages/react-debug-tools/src/ReactDebugHooks.js Outdated Show resolved Hide resolved
packages/react-debug-tools/src/ReactDebugHooks.js Outdated Show resolved Hide resolved
packages/react-devtools-shared/src/backend/renderer.js Outdated Show resolved Hide resolved
@bvaughn
Copy link
Contributor

bvaughn commented Sep 4, 2020

A change like this should probably have several new unit tests as well (for both react-debug-tools and React DevTools). You down to include those with this PR?

I'm going to add test coverage for the DevTools side of this:
#19774

For now, I'll have to disable the function + legacy context tests though.

@bl00mber bl00mber force-pushed the devtools/support-contextTypes branch from 1689628 to bc8cddd Compare September 4, 2020 22:17
@bl00mber
Copy link
Contributor Author

bl00mber commented Sep 6, 2020

Added a test which fails when PR contents are disabled

Comment on lines 372 to 373
const hasLegacyContext = !!childFiber.elementType.contextTypes;
if (hasLegacyContext) {
Copy link
Contributor

@bvaughn bvaughn Sep 8, 2020

Choose a reason for hiding this comment

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

Why is the test doing this check? Seems kind of like lower-level implementation details, and could also lead to a false positive (e.g. replace with !!childFiber.elementType.contextTypess above and the test would still pass because the boolean check would just fail silently).

Since we know the legacy context is being used here, why do we need a conditional at all?

The test also doesn't verify that the expected context value gets injected.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That was my probably inappropriate attempt to prevent test from running on versions where context should be deprecated.
Removed a conditional & added value verification.

@bvaughn bvaughn assigned bvaughn and unassigned bl00mber Sep 14, 2020
Copy link
Contributor

@bvaughn bvaughn left a comment

Choose a reason for hiding this comment

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

I think the changes on the DevTools side look fine, but I'm still a little concerned about the added complexity from this changes to react-debug-tools (and the fact that it adds an optional parameter to the exported inspectHooks method that react-debug-tools will need to continue supporting in future releases– unless we break with a major bump).

What are your thoughts about this, @sebmarkbage?

bvaughn
bvaughn previously approved these changes Sep 14, 2020
@bvaughn bvaughn dismissed their stale review September 14, 2020 13:24

Didn't mean to approve.

@stale
Copy link

stale bot commented Dec 25, 2020

This pull request has been automatically marked as stale. If this pull request is still relevant, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize reviewing it yet. Your contribution is very much appreciated.

@stale stale bot added the Resolution: Stale Automatically closed due to inactivity label Dec 25, 2020
@stale stale bot removed the Resolution: Stale Automatically closed due to inactivity label Jul 15, 2021
@bvaughn bvaughn changed the base branch from master to main July 15, 2021 00:46
@stale
Copy link

stale bot commented Jan 9, 2022

This pull request has been automatically marked as stale. If this pull request is still relevant, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize reviewing it yet. Your contribution is very much appreciated.

@stale stale bot added the Resolution: Stale Automatically closed due to inactivity label Jan 9, 2022
@sebmarkbage sebmarkbage removed their assignment Sep 30, 2022
@stale stale bot removed the Resolution: Stale Automatically closed due to inactivity label Sep 30, 2022
@bl00mber bl00mber closed this Dec 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

react-debug-tools doesn't support legacy context (Component.contextTypes)
7 participants