The Wayback Machine - https://web.archive.org/web/20220424150916/https://github.com/facebook/react/pull/24421
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

forwardRef et al shouldn't affect if props object is reused #24421

Merged
merged 1 commit into from Apr 22, 2022

Conversation

Copy link
Member

@acdlite acdlite commented Apr 21, 2022

We don't have strong guarantees that the props object is referentially equal during updates where we can't bail out anyway — like if the props are shallowly equal, but there's a local state or context update in the same batch.

However, as a principle, we should aim to make the behavior consistent across different ways of memoizing a component. For example, React.memo has a different internal Fiber layout if you pass a normal function component (SimpleMemoComponent) versus if you pass a different type like forwardRef (MemoComponent). But this is an implementation detail. Wrapping a component in forwardRef (or React.lazy, etc) shouldn't affect whether the props object is reused during a bailout.

We don't have strong guarantees that the props object is referentially
equal during updates where we can't bail out anyway — like if the props
are shallowly equal, but there's a local state or context update in the
same batch.

However, as a principle, we should aim to make the behavior consistent
across different ways of memoizing a component. For example, React.memo
has a different internal Fiber layout if you pass a normal function
component (SimpleMemoComponent) versus if you pass a different type like
forwardRef (MemoComponent). But this is an implementation detail.
Wrapping a component in forwardRef (or React.lazy, etc) shouldn't affect
whether the props object is reused during a bailout.

Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
@facebook-github-bot facebook-github-bot added CLA Signed React Core Team labels Apr 21, 2022
@sizebot
Copy link

@sizebot sizebot commented Apr 21, 2022

Comparing: bd08137...3b190a6

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.02% 131.53 kB 131.56 kB +0.03% 42.11 kB 42.12 kB
oss-experimental/react-dom/cjs/react-dom.production.min.js +0.02% 136.80 kB 136.83 kB +0.01% 43.68 kB 43.69 kB
facebook-www/ReactDOM-prod.classic.js +0.03% 441.05 kB 441.18 kB +0.03% 80.44 kB 80.46 kB
facebook-www/ReactDOM-prod.modern.js +0.03% 426.30 kB 426.43 kB +0.03% 78.26 kB 78.28 kB
facebook-www/ReactDOMForked-prod.classic.js +0.03% 441.05 kB 441.18 kB +0.03% 80.44 kB 80.46 kB

Significant size changes

Includes any change greater than 0.2%:

(No significant changes)

Generated by 🚫 dangerJS against 3b190a6

// like forwardRef (MemoComponent). But this is an implementation detail.
// Wrapping a component in forwardRef (or React.lazy, etc) shouldn't
// affect whether the props object is reused during a bailout.
workInProgress.pendingProps = nextProps = prevProps;
Copy link
Contributor

@Andarist Andarist Apr 21, 2022

Choose a reason for hiding this comment

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

nice! so in a way, my fix was same(ish):

workInProgress.pendingProps = nextProps = shallowEqual(prevProps, nextProps)
? prevProps
: nextProps;

this one is "just" applied at a more proper, generic, level 👍

Copy link
Member Author

@acdlite acdlite Apr 22, 2022

Choose a reason for hiding this comment

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

yeah it's essentially the same, I just kept the shallowEqual call where it already was

@acdlite acdlite merged commit 6d3b6d0 into facebook:main Apr 22, 2022
36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed React Core Team
5 participants