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

Double Invoke Effects in __DEV__ #19523

Merged
merged 1 commit into from
Sep 24, 2020
Merged

Conversation

lunaruan
Copy link
Contributor

@lunaruan lunaruan commented Aug 4, 2020

This PR double invokes effects in __DEV__ mode.

We are thinking about unmounting layout and/or passive effects for a hidden tree. To understand potential issues with this, we want to double invoke effects. This PR changes the behavior in DEV when an effect runs from create() to create() -> destroy() -> create(). The effect cleanup function will still be called before the effect runs in both dev and prod. (Note: This change is purely for research for now as it is likely to break real code.)

Note: The change is fully behind a flag and does not affect any of the code on npm.

@facebook-github-bot facebook-github-bot added CLA Signed React Core Team Opened by a member of the React Core Team labels Aug 4, 2020
@codesandbox-ci
Copy link

codesandbox-ci bot commented Aug 4, 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 b9b5750:

Sandbox Source
React Configuration

@sizebot
Copy link

sizebot commented Aug 4, 2020

Details of bundled changes.

Comparing: a99bf5c...b9b5750

react-dom

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-dom.development.js 0.0% 0.0% 872.83 KB 872.83 KB 199.89 KB 199.9 KB NODE_DEV
ReactDOMForked-prod.js 0.0% 0.0% 386.13 KB 386.14 KB 71.33 KB 71.33 KB FB_WWW_PROD
react-dom-server.node.development.js 0.0% 0.0% 137.06 KB 137.06 KB 36.37 KB 36.37 KB NODE_DEV
react-dom.production.min.js 0.0% 0.0% 117.72 KB 117.72 KB 38.02 KB 38.03 KB NODE_PROD
ReactDOMForked-profiling.js 0.0% 0.0% 400.17 KB 400.18 KB 73.73 KB 73.73 KB FB_WWW_PROFILING
react-dom-server.browser.development.js 0.0% 0.0% 143.14 KB 143.14 KB 36.57 KB 36.57 KB UMD_DEV
react-dom-server.node.production.min.js 0.0% 0.0% 20.2 KB 20.2 KB 7.58 KB 7.58 KB NODE_PROD
ReactDOMTesting-dev.js 0.0% 0.0% 940.45 KB 940.45 KB 210.76 KB 210.76 KB FB_WWW_DEV
react-dom-test-utils.development.js 0.0% 0.0% 65.05 KB 65.05 KB 18.62 KB 18.62 KB NODE_DEV
ReactDOMTesting-prod.js 0.0% 0.0% 385.05 KB 385.05 KB 72.38 KB 72.38 KB FB_WWW_PROD
react-dom-test-utils.production.min.js 0.0% -0.0% 13.58 KB 13.58 KB 5.21 KB 5.21 KB NODE_PROD
ReactTestUtils-dev.js +0.1% +0.1% 59.7 KB 59.78 KB 16.56 KB 16.57 KB FB_WWW_DEV
react-dom.development.js 0.0% 0.0% 917.24 KB 917.24 KB 202.43 KB 202.43 KB UMD_DEV
react-dom.production.min.js 0.0% 0.0% 117.62 KB 117.62 KB 38.71 KB 38.71 KB UMD_PROD
react-dom.profiling.min.js 0.0% 0.0% 121.51 KB 121.51 KB 39.92 KB 39.92 KB UMD_PROFILING
ReactDOMForked-dev.js +0.6% +0.4% 988.64 KB 994.78 KB 219.16 KB 219.99 KB FB_WWW_DEV
react-dom.profiling.min.js 0.0% 0.0% 121.79 KB 121.79 KB 39.22 KB 39.22 KB NODE_PROFILING
react-dom-server.browser.production.min.js 0.0% 0.0% 19.88 KB 19.88 KB 7.46 KB 7.46 KB UMD_PROD
ReactDOM-dev.js 0.0% 0.0% 986.84 KB 986.92 KB 219.32 KB 219.34 KB FB_WWW_DEV
ReactDOM-prod.js 0.0% 0.0% 385.83 KB 385.83 KB 71.1 KB 71.1 KB FB_WWW_PROD
react-dom-server.browser.development.js 0.0% 0.0% 135.79 KB 135.79 KB 36.12 KB 36.12 KB NODE_DEV
ReactDOM-profiling.js 0.0% 0.0% 399.04 KB 399.04 KB 73.53 KB 73.53 KB FB_WWW_PROFILING
react-dom-server.browser.production.min.js 0.0% 0.0% 19.78 KB 19.78 KB 7.42 KB 7.42 KB NODE_PROD
ReactDOMServer-dev.js +0.1% +0.1% 145.53 KB 145.61 KB 37.29 KB 37.31 KB FB_WWW_DEV
react-dom-test-utils.development.js 0.0% 0.0% 70.21 KB 70.21 KB 19.11 KB 19.11 KB UMD_DEV

Size changes (stable)

Generated by 🚫 dangerJS against b9b5750

@sizebot
Copy link

sizebot commented Aug 4, 2020

Details of bundled changes.

Comparing: a99bf5c...b9b5750

react-dom

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-dom.development.js 0.0% 0.0% 908.28 KB 908.28 KB 206.4 KB 206.4 KB NODE_DEV
ReactDOMForked-prod.js 0.0% 0.0% 374.8 KB 374.81 KB 69.54 KB 69.54 KB FB_WWW_PROD
react-dom-server.node.development.js 0.0% 0.0% 138.57 KB 138.57 KB 36.58 KB 36.59 KB NODE_DEV
react-dom.production.min.js 0.0% 0.0% 122.28 KB 122.28 KB 39.34 KB 39.34 KB NODE_PROD
ReactDOMForked-profiling.js 0.0% 0.0% 388.8 KB 388.81 KB 72 KB 72 KB FB_WWW_PROFILING
react-dom-server.browser.development.js 0.0% 0.0% 144.73 KB 144.73 KB 36.77 KB 36.77 KB UMD_DEV
react-dom-server.node.production.min.js 0.0% 0.0% 20.66 KB 20.66 KB 7.65 KB 7.65 KB NODE_PROD
ReactDOMTesting-dev.js 0.0% 0.0% 912.12 KB 912.12 KB 205.29 KB 205.29 KB FB_WWW_DEV
react-dom-test-utils.development.js 0.0% 0.0% 65.06 KB 65.06 KB 18.63 KB 18.63 KB NODE_DEV
react-dom-unstable-fizz.node.development.js 0.0% +0.1% 5.52 KB 5.52 KB 1.84 KB 1.84 KB NODE_DEV
react-dom-test-utils.production.min.js 0.0% -0.0% 13.59 KB 13.59 KB 5.22 KB 5.22 KB NODE_PROD
react-dom-unstable-fizz.browser.development.js 0.0% +0.1% 5.25 KB 5.25 KB 1.78 KB 1.78 KB UMD_DEV
react-dom-unstable-fizz.node.production.min.js 0.0% 🔺+0.3% 1.17 KB 1.17 KB 666 B 668 B NODE_PROD
react-dom-unstable-fizz.browser.production.min.js 0.0% 🔺+0.1% 1.22 KB 1.22 KB 712 B 713 B UMD_PROD
ReactTestUtils-dev.js +0.1% +0.1% 59.7 KB 59.78 KB 16.56 KB 16.58 KB FB_WWW_DEV
react-dom-unstable-fizz.browser.development.js 0.0% +0.1% 4.78 KB 4.78 KB 1.68 KB 1.68 KB NODE_DEV
react-dom.development.js 0.0% 0.0% 954.43 KB 954.43 KB 209.01 KB 209.01 KB UMD_DEV
react-dom-unstable-fizz.browser.production.min.js 0.0% 🔺+0.2% 1.01 KB 1.01 KB 616 B 617 B NODE_PROD
react-dom.production.min.js 0.0% 0.0% 122.11 KB 122.11 KB 40.09 KB 40.09 KB UMD_PROD
react-dom.profiling.min.js 0.0% 0.0% 127.36 KB 127.36 KB 41.76 KB 41.76 KB UMD_PROFILING
ReactDOMForked-dev.js +0.6% +0.4% 963.06 KB 969.19 KB 214.44 KB 215.27 KB FB_WWW_DEV
react-dom.profiling.min.js 0.0% 0.0% 127.72 KB 127.72 KB 41.01 KB 41.02 KB NODE_PROFILING
ReactDOM-dev.js 0.0% 0.0% 961.26 KB 961.34 KB 214.66 KB 214.67 KB FB_WWW_DEV
ReactDOM-prod.js 0.0% -0.0% 374.58 KB 374.58 KB 69.39 KB 69.39 KB FB_WWW_PROD
react-dom-server.browser.development.js 0.0% 0.0% 137.3 KB 137.3 KB 36.33 KB 36.33 KB NODE_DEV
ReactDOM-profiling.js 0.0% 0.0% 387.74 KB 387.74 KB 71.82 KB 71.83 KB FB_WWW_PROFILING
react-dom-server.browser.production.min.js 0.0% 0.0% 20.24 KB 20.24 KB 7.5 KB 7.5 KB NODE_PROD
ReactDOMServer-dev.js +0.1% +0.1% 141.5 KB 141.58 KB 36.28 KB 36.3 KB FB_WWW_DEV
ReactDOMServer-prod.js 0.0% 0.0% 46.44 KB 46.44 KB 10.83 KB 10.83 KB FB_WWW_PROD
react-dom-test-utils.development.js 0.0% 0.0% 70.22 KB 70.22 KB 19.12 KB 19.12 KB UMD_DEV

Size changes (experimental)

Generated by 🚫 dangerJS against b9b5750

@bvaughn bvaughn self-assigned this Aug 4, 2020
@oriSomething
Copy link

oriSomething commented Aug 6, 2020

It can cause some issues. For example, in where I work we have a component that have this kind of effect:

React.useEffect(() => {
  navigator.clipboard.readText().then(/* …And content to state variable  */);
}, []);

Also some components that sends analytics / fetch some data, which will probably break many tests.

EDIT:
We don't have proper cleanups always (What I wanted to say in short)

@bvaughn
Copy link
Contributor

bvaughn commented Aug 6, 2020

Also some components that sends analytics / fetch some data

I'm not sure if this is what you're saying, but you probably shouldn't be sending analytics in DEV mode (or you should be sending them somewhere different than production analytics).

As for your concern about testing, that's something I haven't thought much about. I can see where this might cause hassle.

@oriSomething
Copy link

@bvaughn Well, the analytics is not about sending them. We have a logger of analytics (to browsers devtools console), so while developing we can make sure they logged in correct scenarios. If sometimes it called twice and sometimes once, we can't predict if analytics correct.

@vkurchatkin
Copy link

This looks like it might brake a lot of code. This changes implies that effects can only be used when they have very specific semantics: they should either be idempotent or reversible. Also it makes it a requirement to implement a cleanup even if it is never supposed to run.

@gaearon
Copy link
Collaborator

gaearon commented Aug 6, 2020

The change is fully behind a flag and does not affect any of the code on npm. Indeed, it is likely to break some code, so we want to try it at FB to see how large the impact is. We appreciate your concern but researching this more is the whole point of this PR. Hope this makes sense.

@gaearon
Copy link
Collaborator

gaearon commented Aug 6, 2020

I'll add though that [] effects are already invoked multiple times when you use Fast Refresh, for example. So it's a good idea to make things that truly must only happen once resistant to this, e.g. using a ref. Again, coming up with a specific guidance is still a part of the research. We need to balance the code that's already written with the tradeoffs of adding new valuable features.

@lunaruan lunaruan force-pushed the double_invoke branch 3 times, most recently from fe64e03 to e51e75b Compare August 12, 2020 23:28
@lunaruan
Copy link
Contributor Author

Ready for review!

@lunaruan
Copy link
Contributor Author

Updated the PR based on everyone's comments!

let fiber = firstChild;
while (fiber !== null) {
if (fiber.child !== null) {
// Should we add a separate subtree tag for this?
Copy link
Contributor

Choose a reason for hiding this comment

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

What's this comment for? I'm not sure I understand it.

Is this pointing out the fact that we're going to end up traversing all of the subtree paths that have layout effects even if they're all updates and none of them are mounts? So we could add a DEV-only tag e.g. MountLayoutSubtreeTag or something.

Maybe we could add some additional clarification to the wording here.

Copy link
Contributor Author

@lunaruan lunaruan Aug 22, 2020

Choose a reason for hiding this comment

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

Yeah, I wasn't sure how OK it was to add a subtree tag vs. just traversing extra in DEV since it takes extra bytes to add an extra subtreetag, and I know at least with effectTag, we don't seem to want to add too many. If it's in DEV though it makes sense that it's OK. Thanks for clarifying!

packages/react-reconciler/src/ReactFiberCommitWork.new.js Outdated Show resolved Hide resolved
packages/react-reconciler/src/ReactFiberCommitWork.new.js Outdated Show resolved Hide resolved
packages/react-reconciler/src/ReactFiberCommitWork.new.js Outdated Show resolved Hide resolved
packages/react-reconciler/src/ReactFiberCommitWork.new.js Outdated Show resolved Hide resolved
case ForwardRef:
case SimpleMemoComponent:
case Block: {
invokeGuardedCallback(
Copy link
Collaborator

@sebmarkbage sebmarkbage Aug 14, 2020

Choose a reason for hiding this comment

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

Technically, after https://github.com/facebook/react/pull/19605/files this will be unnecessary since commitHookEffectListUnmount should be safe now.

It's an awkward inconsistency since the equivalent mount isn't safe. I don't know what the answer is.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm I'll leave it as is for now for consistency and maybe do a separate PR to make everything more consistent (Will creating a safelyCallCreate function cause perf issues)?

This was referenced Nov 4, 2024
This was referenced Nov 6, 2024
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.

9 participants