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

Fix: Class components should "consume" ref prop #28719

Merged
merged 3 commits into from
Apr 3, 2024

Conversation

acdlite
Copy link
Collaborator

@acdlite acdlite commented Apr 2, 2024

When a ref is passed to a class component, the class instance is attached to the ref's current property automatically. This different from function components, where you have to do something extra to attach a ref to an instance, like passing the ref to useImperativeHandle.

Existing class component code is written with the assumption that a ref will not be passed through as a prop. For example, class components that act as indirections often spread this.props onto a child component. To maintain this expectation, we should remove the ref from the props object ("consume" it) before passing it to lifecycle methods. Without this change, much existing code will break because the ref will attach to the inner component instead of the outer one.

This is not an issue for function components because we used to warn if you passed a ref to a function component. Instead, you had to use forwardRef, which also implements this "consuming" behavior.

There are a few places in the reconciler where we modify the fiber's internal props object before passing it to userspace. The trickiest one is class components, because the props object gets exposed in many different places, including as a property on the class instance.

This was already accounted for when we added support for setting default props on a lazy wrapper (i.e. React.lazy that resolves to a class component).

In all of these same places, we will also need to remove the ref prop when enableRefAsProp is on.

Closes #28602

@facebook-github-bot facebook-github-bot added CLA Signed React Core Team Opened by a member of the React Core Team labels Apr 2, 2024
@react-sizebot
Copy link

react-sizebot commented Apr 2, 2024

Comparing: 8f55a6a...bc1fac0

Critical size changes

Includes critical production bundles, as well as any change greater than 2%:

Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable/react-dom/cjs/react-dom.production.min.js +0.09% 173.32 kB 173.47 kB +0.16% 53.93 kB 54.01 kB
oss-experimental/react-dom/cjs/react-dom.production.min.js +0.11% 170.94 kB 171.13 kB +0.14% 53.25 kB 53.33 kB
facebook-www/ReactDOM-prod.classic.js +0.13% 588.57 kB 589.36 kB +0.21% 103.51 kB 103.73 kB
facebook-www/ReactDOM-prod.modern.js = 566.68 kB 566.39 kB +0.12% 99.52 kB 99.64 kB
test_utils/ReactAllWarnings.js Deleted 64.58 kB 0.00 kB Deleted 16.14 kB 0.00 kB

Significant size changes

Includes any change greater than 0.2%:

Expand to show
Name +/- Base Current +/- gzip Base gzip Current gzip
oss-experimental/react-art/cjs/react-art.production.js +0.34% 588.85 kB 590.84 kB +0.36% 131.90 kB 132.38 kB
oss-experimental/react-test-renderer/cjs/react-test-renderer.production.js +0.34% 590.65 kB 592.65 kB +0.35% 132.50 kB 132.97 kB
oss-stable-semver/react-art/cjs/react-art.production.js +0.30% 591.34 kB 593.12 kB +0.33% 132.46 kB 132.89 kB
oss-stable/react-art/cjs/react-art.production.js +0.30% 591.37 kB 593.15 kB +0.33% 132.48 kB 132.92 kB
oss-stable-semver/react-test-renderer/cjs/react-test-renderer.production.js +0.30% 599.86 kB 601.64 kB +0.33% 134.75 kB 135.19 kB
oss-stable/react-test-renderer/cjs/react-test-renderer.production.js +0.30% 599.88 kB 601.66 kB +0.33% 134.77 kB 135.22 kB
oss-experimental/react-reconciler/cjs/react-reconciler.production.js +0.30% 672.55 kB 674.54 kB +0.32% 148.05 kB 148.52 kB
facebook-react-native/react-test-renderer/cjs/ReactTestRenderer-profiling.js +0.28% 339.30 kB 340.26 kB +0.24% 59.00 kB 59.14 kB
oss-experimental/react-reconciler/cjs/react-reconciler.profiling.js +0.28% 712.57 kB 714.57 kB +0.29% 155.31 kB 155.76 kB
oss-stable-semver/react-reconciler/cjs/react-reconciler.production.js +0.26% 675.05 kB 676.83 kB +0.28% 148.54 kB 148.95 kB
oss-stable/react-reconciler/cjs/react-reconciler.production.js +0.26% 675.07 kB 676.85 kB +0.28% 148.57 kB 148.99 kB
oss-experimental/react-test-renderer/cjs/react-test-renderer.development.js +0.26% 804.52 kB 806.61 kB +0.28% 175.29 kB 175.79 kB
oss-experimental/react-test-renderer/umd/react-test-renderer.development.js +0.26% 842.76 kB 844.94 kB +0.29% 177.25 kB 177.77 kB
facebook-react-native/react-test-renderer/cjs/ReactTestRenderer-prod.js +0.26% 315.59 kB 316.40 kB +0.25% 55.56 kB 55.70 kB
oss-experimental/react-art/cjs/react-art.development.js +0.26% 822.47 kB 824.56 kB +0.29% 178.07 kB 178.59 kB
oss-stable-semver/react-reconciler/cjs/react-reconciler.profiling.js +0.25% 715.07 kB 716.85 kB +0.25% 155.80 kB 156.19 kB
oss-stable/react-reconciler/cjs/react-reconciler.profiling.js +0.25% 715.09 kB 716.87 kB +0.25% 155.82 kB 156.22 kB
facebook-www/ReactART-prod.classic.js +0.24% 364.82 kB 365.70 kB +0.30% 61.64 kB 61.83 kB
facebook-www/ReactART-dev.modern.js +0.24% 1,045.17 kB 1,047.70 kB +0.28% 204.82 kB 205.39 kB
facebook-react-native/react-test-renderer/cjs/ReactTestRenderer-dev.js +0.23% 960.40 kB 962.64 kB +0.25% 189.46 kB 189.93 kB
facebook-www/ReactART-dev.classic.js +0.23% 1,078.84 kB 1,081.35 kB +0.26% 210.93 kB 211.49 kB
facebook-www/ReactTestRenderer-dev.modern.js +0.23% 965.34 kB 967.59 kB +0.25% 191.69 kB 192.16 kB
facebook-www/ReactTestRenderer-dev.classic.js +0.23% 965.35 kB 967.59 kB +0.25% 191.69 kB 192.16 kB
oss-experimental/react-art/umd/react-art.development.js +0.23% 938.31 kB 940.49 kB +0.27% 197.30 kB 197.82 kB
oss-stable-semver/react-test-renderer/cjs/react-test-renderer.development.js +0.23% 816.28 kB 818.17 kB +0.27% 177.93 kB 178.41 kB
oss-stable/react-test-renderer/cjs/react-test-renderer.development.js +0.23% 816.31 kB 818.19 kB +0.27% 177.96 kB 178.44 kB
oss-experimental/react-reconciler/cjs/react-reconciler.development.js +0.23% 919.54 kB 921.64 kB +0.26% 197.66 kB 198.16 kB
react-native/implementations/ReactFabric-dev.js +0.23% 983.99 kB 986.23 kB +0.23% 196.02 kB 196.47 kB
oss-stable-semver/react-test-renderer/umd/react-test-renderer.development.js +0.23% 855.04 kB 856.99 kB +0.25% 179.85 kB 180.29 kB
oss-stable/react-test-renderer/umd/react-test-renderer.development.js +0.23% 855.06 kB 857.01 kB +0.25% 179.88 kB 180.32 kB
oss-stable-semver/react-art/cjs/react-art.development.js +0.23% 830.69 kB 832.58 kB +0.26% 180.20 kB 180.66 kB
oss-stable/react-art/cjs/react-art.development.js +0.23% 830.72 kB 832.60 kB +0.26% 180.23 kB 180.69 kB
react-native/implementations/ReactNativeRenderer-dev.js +0.22% 998.89 kB 1,001.13 kB +0.23% 199.75 kB 200.22 kB
react-native/implementations/ReactFabric-prod.fb.js +0.22% 366.38 kB 367.20 kB +0.23% 64.14 kB 64.29 kB
react-native/implementations/ReactNativeRenderer-prod.fb.js +0.22% 373.78 kB 374.59 kB +0.23% 65.42 kB 65.57 kB
react-native/implementations/ReactFabric-profiling.fb.js +0.22% 393.58 kB 394.43 kB +0.22% 68.34 kB 68.49 kB
oss-experimental/react-dom/cjs/react-dom.production.js +0.22% 925.64 kB 927.63 kB +0.22% 208.48 kB 208.93 kB
react-native/implementations/ReactNativeRenderer-profiling.fb.js +0.21% 401.04 kB 401.89 kB +0.20% 69.67 kB 69.81 kB
oss-experimental/react-dom/cjs/react-dom-unstable_testing.production.js +0.21% 943.88 kB 945.88 kB +0.21% 212.96 kB 213.41 kB
oss-experimental/react-dom/cjs/react-dom.profiling.js +0.21% 966.25 kB 968.24 kB +0.20% 215.81 kB 216.25 kB
oss-stable-semver/react-art/umd/react-art.development.js +0.21% 946.87 kB 948.82 kB +0.23% 199.43 kB 199.88 kB
oss-stable/react-art/umd/react-art.development.js +0.21% 946.90 kB 948.85 kB +0.23% 199.46 kB 199.91 kB
react-native/implementations/ReactFabric-dev.fb.js +0.20% 1,096.70 kB 1,098.94 kB +0.22% 217.45 kB 217.93 kB
oss-stable-semver/react-reconciler/cjs/react-reconciler.development.js +0.20% 925.17 kB 927.05 kB +0.23% 198.72 kB 199.17 kB
oss-stable/react-reconciler/cjs/react-reconciler.development.js +0.20% 925.19 kB 927.08 kB +0.23% 198.74 kB 199.20 kB
oss-experimental/react-reconciler/cjs/react-reconciler.profiling.min.js +0.20% 119.89 kB 120.13 kB +0.28% 36.69 kB 36.79 kB
react-native/implementations/ReactNativeRenderer-dev.fb.js +0.20% 1,111.31 kB 1,113.55 kB +0.22% 221.30 kB 221.78 kB
oss-experimental/react-art/cjs/react-art.production.min.js +0.20% 96.57 kB 96.77 kB +0.30% 29.67 kB 29.76 kB
test_utils/ReactAllWarnings.js Deleted 64.58 kB 0.00 kB Deleted 16.14 kB 0.00 kB

Generated by 🚫 dangerJS against bc1fac0

@acdlite acdlite force-pushed the fix-class-consume-ref-prop branch 2 times, most recently from ec53821 to a550570 Compare April 2, 2024 17:50
@acdlite acdlite marked this pull request as ready for review April 2, 2024 17:55
@acdlite acdlite requested a review from kassens April 2, 2024 17:55
Copy link
Member

@kassens kassens left a comment

Choose a reason for hiding this comment

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

Without this change, much existing code will break because the ref will attach to the inner component instead of the outer one.

Actually worse, it'd assign the ref to both. A callback ref would actually observe both bindings.

const prevState = current.memoizedState;
// We could update instance props and state here,
// but instead we rely on them being set during last render.
// TODO: revisit this when we implement resuming.
if (__DEV__) {
if (
finishedWork.type === finishedWork.elementType &&
!finishedWork.type.defaultProps &&
Copy link
Member

Choose a reason for hiding this comment

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

This disables the re-assign props warning when defaultProps are present? Seems fine.

@acdlite acdlite force-pushed the fix-class-consume-ref-prop branch 2 times, most recently from 72e8cf4 to 6450fc6 Compare April 3, 2024 02:39
acdlite and others added 3 commits April 2, 2024 22:59
There are a few places in the reconciler where we modify the fiber's
internal props object before passing it to userspace. The trickiest one
is class components, because the props object gets exposed in many
different places, including as a property on the class instance.

This was already accounted for when we added support for setting
default props on a lazy wrapper (i.e. React.lazy that resolves to a
class component).

In all of these same places, we will also need to remove the ref prop
when enableRefAsProp is on.

As a first step, this adds a new function, resolveClassComponentProps,
where both default prop resolution and ref prop removal will happen.
When a ref is passed to a class component, the class instance is
attached to the ref's current property automatically. This different
from function components, where you have to do something extra to
attach a ref to an instance, like passing the ref
to `useImperativeHandle`.

Existing class component code is written with the assumption that a
ref will not be passed through as a prop. For example, class components
that act as indirections often spread `this.props` onto a child
component. To maintain this expectation, we should remove the ref from
the props object ("consume" it) before passing it to lifecycle methods.
Without this change, much existing code will break because the ref will
attach to the inner component instead of the outer one.

This is not an issue for function components because we used to warn if
you passed a ref to a function component. Instead, you had to use
`forwardRef`, which also implements this "consuming" behavior.

Co-authored-by: Jan Kassens <jan@kassens.net>
Noticed these once I enabled class prop resolution in more places.
Technically this was already observable if you wrapped a class with
`React.lazy` and gave that wrapper default props, but since that was so
rare it was never reported.
@acdlite acdlite force-pushed the fix-class-consume-ref-prop branch from 6450fc6 to bc1fac0 Compare April 3, 2024 03:02
@acdlite acdlite merged commit dc545c8 into facebook:main Apr 3, 2024
38 checks passed
github-actions bot pushed a commit that referenced this pull request Apr 3, 2024
When a ref is passed to a class component, the class instance is
attached to the ref's current property automatically. This different
from function components, where you have to do something extra to attach
a ref to an instance, like passing the ref to `useImperativeHandle`.

Existing class component code is written with the assumption that a ref
will not be passed through as a prop. For example, class components that
act as indirections often spread `this.props` onto a child component. To
maintain this expectation, we should remove the ref from the props
object ("consume" it) before passing it to lifecycle methods. Without
this change, much existing code will break because the ref will attach
to the inner component instead of the outer one.

This is not an issue for function components because we used to warn if
you passed a ref to a function component. Instead, you had to use
`forwardRef`, which also implements this "consuming" behavior.

There are a few places in the reconciler where we modify the fiber's
internal props object before passing it to userspace. The trickiest one
is class components, because the props object gets exposed in many
different places, including as a property on the class instance.

This was already accounted for when we added support for setting default
props on a lazy wrapper (i.e. `React.lazy` that resolves to a class
component).

In all of these same places, we will also need to remove the ref prop
when `enableRefAsProp` is on.

Closes #28602

---------

Co-authored-by: Jan Kassens <jan@kassens.net>

DiffTrain build for [dc545c8](dc545c8)
acdlite added a commit to acdlite/react that referenced this pull request Apr 3, 2024
acdlite added a commit to acdlite/react that referenced this pull request Apr 3, 2024
acdlite added a commit that referenced this pull request Apr 3, 2024
github-actions bot pushed a commit that referenced this pull request Apr 3, 2024
Same as #28719 but for SSR.

DiffTrain build for [3761acb](3761acb)
EdisonVan pushed a commit to EdisonVan/react that referenced this pull request Apr 15, 2024
When a ref is passed to a class component, the class instance is
attached to the ref's current property automatically. This different
from function components, where you have to do something extra to attach
a ref to an instance, like passing the ref to `useImperativeHandle`.

Existing class component code is written with the assumption that a ref
will not be passed through as a prop. For example, class components that
act as indirections often spread `this.props` onto a child component. To
maintain this expectation, we should remove the ref from the props
object ("consume" it) before passing it to lifecycle methods. Without
this change, much existing code will break because the ref will attach
to the inner component instead of the outer one.

This is not an issue for function components because we used to warn if
you passed a ref to a function component. Instead, you had to use
`forwardRef`, which also implements this "consuming" behavior.

There are a few places in the reconciler where we modify the fiber's
internal props object before passing it to userspace. The trickiest one
is class components, because the props object gets exposed in many
different places, including as a property on the class instance.

This was already accounted for when we added support for setting default
props on a lazy wrapper (i.e. `React.lazy` that resolves to a class
component).

In all of these same places, we will also need to remove the ref prop
when `enableRefAsProp` is on.

Closes facebook#28602

---------

Co-authored-by: Jan Kassens <jan@kassens.net>
EdisonVan pushed a commit to EdisonVan/react that referenced this pull request Apr 15, 2024
bigfootjon pushed a commit that referenced this pull request Apr 18, 2024
When a ref is passed to a class component, the class instance is
attached to the ref's current property automatically. This different
from function components, where you have to do something extra to attach
a ref to an instance, like passing the ref to `useImperativeHandle`.

Existing class component code is written with the assumption that a ref
will not be passed through as a prop. For example, class components that
act as indirections often spread `this.props` onto a child component. To
maintain this expectation, we should remove the ref from the props
object ("consume" it) before passing it to lifecycle methods. Without
this change, much existing code will break because the ref will attach
to the inner component instead of the outer one.

This is not an issue for function components because we used to warn if
you passed a ref to a function component. Instead, you had to use
`forwardRef`, which also implements this "consuming" behavior.

There are a few places in the reconciler where we modify the fiber's
internal props object before passing it to userspace. The trickiest one
is class components, because the props object gets exposed in many
different places, including as a property on the class instance.

This was already accounted for when we added support for setting default
props on a lazy wrapper (i.e. `React.lazy` that resolves to a class
component).

In all of these same places, we will also need to remove the ref prop
when `enableRefAsProp` is on.

Closes #28602

---------

Co-authored-by: Jan Kassens <jan@kassens.net>

DiffTrain build for commit dc545c8.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed React Core Team Opened by a member of the React Core Team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants