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

Class components should consume the ref prop #28602

Closed
wants to merge 1 commit into from

Conversation

kassens
Copy link
Member

@kassens kassens commented Mar 20, 2024

Class components should consume the ref prop

With the enableRefAsProp flag enabled, refs are normal props and no longer filtered in the JSX runtime. Still, some APIs exist that conceptionally "consume" the ref since they bind the ref to a value. This includes forwardRef that already implemented filtering the ref prop out to the props passed to the inner component. We also need to do the same for class components. A ref passed a class component is bound to that class instance, if we keep the ref unfiltered and the component spreads all the props to a child component the ref would see 2 or more values set to it.

With the `enableRefAsProp` flag enabled, refs are normal props and no longer filtered in the JSX runtime. Still, some APIs exist that conceptionally "consume" the ref since they bind the ref to a value. This includes `forwardRef` that already implemented filtering the `ref` prop out to the props passed to the inner component. We also need to do the same for class components. A `ref` passed a class component is bound to that class instance, if we keep the ref unfiltered and the component spreads all the props to a child component the `ref` would see 2 or more values set to it.
@kassens kassens requested a review from sebmarkbage March 20, 2024 21:52
@facebook-github-bot facebook-github-bot added the React Core Team Opened by a member of the React Core Team label Mar 20, 2024
@kassens kassens requested a review from acdlite March 20, 2024 21:53
@react-sizebot
Copy link

Comparing: a493901...5ae75aa

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 = 176.84 kB 176.84 kB = 54.92 kB 54.92 kB
oss-experimental/react-dom/cjs/react-dom.production.min.js +0.05% 173.27 kB 173.34 kB +0.05% 54.04 kB 54.07 kB
facebook-www/ReactDOM-prod.classic.js +0.06% 594.30 kB 594.64 kB +0.04% 104.45 kB 104.49 kB
facebook-www/ReactDOM-prod.modern.js +0.16% 577.56 kB 578.51 kB +0.06% 101.48 kB 101.55 kB
test_utils/ReactAllWarnings.js Deleted 66.48 kB 0.00 kB Deleted 16.26 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
facebook-www/ReactART-prod.modern.js +0.27% 353.24 kB 354.19 kB +0.13% 59.82 kB 59.90 kB
test_utils/ReactAllWarnings.js Deleted 66.48 kB 0.00 kB Deleted 16.26 kB 0.00 kB

Generated by 🚫 dangerJS against 5ae75aa

@kassens kassens marked this pull request as draft March 20, 2024 21:57
@kassens
Copy link
Member Author

kassens commented Apr 2, 2024

Replaced by #28719

@kassens kassens closed this Apr 2, 2024
@kassens kassens deleted the pr28602 branch April 2, 2024 22:25
acdlite added 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>
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)
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>
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.

3 participants