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

[Fresh] Support classes by force-remounting them on edit #15801

Merged
merged 6 commits into from
Jun 5, 2019

Conversation

gaearon
Copy link
Collaborator

@gaearon gaearon commented Jun 2, 2019

The first commit adds support for classes.

We don't need state preservation for classes. But we do need the opposite. When you edit a class, we want to remount its instances (and reset their state).

Remounting seems like it should already work. Because the type changes, and React is doing its thing. However, there is a case where that's not enough. If I edit Button.js that happens to be a class, other modules won't "see" the new export unless the update "propagates upwards". So if Modal.js imports it, we'd have to re-execute Modal.js too. But that might also be a class. So we lose the locality of the edit — and in some cases it's even unsafe to re-execute arbitrary code above. (E.g. that breaks React Native once you get too close to top-level modules.) Inline requires or ES live imports sort of solve that, but not in all cases. Such as a cached element in state that already has a specific type.

To solve that, we can stop update propagation at modules where all exports look like React components. (Functions or classes.) In case of classes, we want to remount all instances — and mount new versions. This should happen even for stale <OldButton /> references in the tree. That's why I added the resolving mechanism to classes too. However, they would never attempt to preserve state.

@sizebot
Copy link

sizebot commented Jun 2, 2019

Details of bundled changes.

Comparing: 73c27d8...f254e3a

react-dom

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-dom.profiling.min.js 0.0% -0.0% 109.24 KB 109.24 KB 34.71 KB 34.7 KB NODE_PROFILING
ReactDOM-dev.js +0.1% 0.0% 883.95 KB 884.5 KB 196.9 KB 196.97 KB FB_WWW_DEV
ReactDOMServer-dev.js 0.0% -0.0% 135.5 KB 135.5 KB 34.81 KB 34.81 KB FB_WWW_DEV
react-dom-unstable-fire.development.js +0.1% 0.0% 864.42 KB 865 KB 197.09 KB 197.14 KB UMD_DEV
react-dom-unstable-fire.profiling.min.js 0.0% -0.0% 109.07 KB 109.07 KB 35.41 KB 35.41 KB UMD_PROFILING
react-dom-unstable-fire.development.js +0.1% 0.0% 858.78 KB 859.36 KB 195.52 KB 195.57 KB NODE_DEV
react-dom-unstable-fire.production.min.js 0.0% -0.0% 105.92 KB 105.92 KB 33.86 KB 33.86 KB NODE_PROD
react-dom-server.node.production.min.js 0.0% -0.0% 19.98 KB 19.98 KB 7.53 KB 7.53 KB NODE_PROD
react-dom.development.js +0.1% 0.0% 864.08 KB 864.66 KB 196.95 KB 197 KB UMD_DEV
react-dom.production.min.js 0.0% -0.0% 105.91 KB 105.91 KB 34.44 KB 34.44 KB UMD_PROD
ReactFire-dev.js +0.1% 0.0% 883.16 KB 883.71 KB 196.83 KB 196.89 KB FB_WWW_DEV
react-dom.profiling.min.js 0.0% -0.0% 109.06 KB 109.06 KB 35.4 KB 35.4 KB UMD_PROFILING
react-dom.development.js +0.1% 0.0% 858.44 KB 859.02 KB 195.38 KB 195.43 KB NODE_DEV
react-dom-server.browser.development.js 0.0% -0.0% 133.41 KB 133.41 KB 35.3 KB 35.3 KB NODE_DEV
react-dom-server.browser.production.min.js 0.0% -0.0% 19.12 KB 19.12 KB 7.22 KB 7.22 KB NODE_PROD
ReactDOM-prod.js 0.0% -0.0% 358.19 KB 358.19 KB 66.14 KB 66.14 KB FB_WWW_PROD
ReactDOMServer-prod.js 0.0% -0.0% 47.93 KB 47.93 KB 11.04 KB 11.03 KB FB_WWW_PROD
ReactDOM-profiling.js 0.0% -0.0% 363.43 KB 363.43 KB 67.03 KB 67.03 KB FB_WWW_PROFILING
react-dom-unstable-native-dependencies.development.js 0.0% -0.0% 60.43 KB 60.43 KB 15.72 KB 15.72 KB NODE_DEV
react-dom-unstable-fizz.node.production.min.js 0.0% 🔺+0.1% 1.1 KB 1.1 KB 667 B 668 B NODE_PROD
react-dom-test-utils.development.js 0.0% -0.0% 57.64 KB 57.64 KB 15.85 KB 15.85 KB UMD_DEV
react-dom-unstable-fizz.browser.development.js 0.0% -0.1% 3.81 KB 3.81 KB 1.54 KB 1.53 KB UMD_DEV
react-dom-unstable-fizz.browser.production.min.js 0.0% -0.1% 1.21 KB 1.21 KB 707 B 706 B UMD_PROD
react-dom-unstable-fizz.browser.development.js 0.0% -0.1% 3.64 KB 3.64 KB 1.49 KB 1.49 KB NODE_DEV

react-art

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
ReactART-dev.js +0.1% +0.1% 535.28 KB 535.83 KB 112.12 KB 112.18 KB FB_WWW_DEV
react-art.development.js +0.1% +0.1% 594.07 KB 594.65 KB 130.35 KB 130.42 KB UMD_DEV
react-art.production.min.js 0.0% -0.0% 97.11 KB 97.11 KB 29.91 KB 29.91 KB UMD_PROD
react-art.development.js +0.1% 0.0% 524.98 KB 525.56 KB 112.88 KB 112.94 KB NODE_DEV
react-art.production.min.js 0.0% -0.0% 62.09 KB 62.09 KB 19.2 KB 19.19 KB NODE_PROD
ReactART-prod.js 0.0% -0.0% 202.95 KB 202.95 KB 34.56 KB 34.56 KB FB_WWW_PROD

react-test-renderer

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-test-renderer.development.js +0.1% +0.1% 538.58 KB 539.16 KB 115.7 KB 115.77 KB UMD_DEV
react-test-renderer.production.min.js 0.0% -0.0% 63.42 KB 63.42 KB 19.64 KB 19.63 KB UMD_PROD
react-test-renderer.development.js +0.1% +0.1% 534.12 KB 534.7 KB 114.6 KB 114.66 KB NODE_DEV
react-test-renderer.production.min.js 0.0% -0.0% 63.11 KB 63.11 KB 19.42 KB 19.42 KB NODE_PROD
ReactTestRenderer-dev.js +0.1% +0.1% 546.59 KB 547.14 KB 114.63 KB 114.69 KB FB_WWW_DEV
react-test-renderer-shallow.development.js 0.0% 0.0% 42.01 KB 42.01 KB 10.89 KB 10.89 KB UMD_DEV
react-test-renderer-shallow.production.min.js 0.0% 0.0% 11.6 KB 11.6 KB 3.55 KB 3.55 KB UMD_PROD
react-test-renderer-shallow.production.min.js 0.0% 0.0% 11.74 KB 11.74 KB 3.67 KB 3.67 KB NODE_PROD

react-native-renderer

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
ReactFabric-dev.js +0.1% +0.1% 658.38 KB 658.93 KB 139.97 KB 140.05 KB RN_FB_DEV
ReactFabric-prod.js 0.0% -0.0% 245.4 KB 245.4 KB 42.51 KB 42.51 KB RN_FB_PROD
ReactNativeRenderer-dev.js +0.1% +0.1% 669.51 KB 670.06 KB 142.6 KB 142.67 KB RN_OSS_DEV
ReactNativeRenderer-dev.js +0.1% +0.1% 669.6 KB 670.15 KB 142.65 KB 142.72 KB RN_FB_DEV
ReactNativeRenderer-profiling.js 0.0% -0.0% 260.74 KB 260.74 KB 45.34 KB 45.34 KB RN_FB_PROFILING
ReactFabric-dev.js +0.1% +0.1% 658.28 KB 658.83 KB 139.94 KB 140.01 KB RN_OSS_DEV

react-reconciler

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-reconciler-reflection.production.min.js 0.0% -0.1% 2.51 KB 2.51 KB 1.12 KB 1.11 KB NODE_PROD
react-reconciler-persistent.development.js +0.1% +0.1% 520.54 KB 521.12 KB 110.29 KB 110.35 KB NODE_DEV
react-reconciler-persistent.production.min.js 0.0% -0.0% 63.27 KB 63.27 KB 19 KB 19 KB NODE_PROD
react-reconciler.development.js +0.1% 0.0% 523.04 KB 523.61 KB 111.37 KB 111.42 KB NODE_DEV
react-reconciler.production.min.js 0.0% -0.0% 63.26 KB 63.26 KB 19 KB 19 KB NODE_PROD

react-fresh

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-fresh-runtime.development.js +12.4% +8.2% 4.62 KB 5.2 KB 1.83 KB 1.98 KB NODE_DEV
react-fresh-runtime.production.min.js 🔺+16.1% 🔺+11.3% 1.34 KB 1.55 KB 767 B 854 B NODE_PROD
react-fresh-babel.development.js +9.0% +11.9% 18.33 KB 19.97 KB 3.76 KB 4.2 KB NODE_DEV
react-fresh-babel.production.min.js 🔺+8.9% 🔺+9.4% 5.35 KB 5.83 KB 1.89 KB 2.07 KB NODE_PROD

Generated by 🚫 dangerJS

@fresonn
Copy link

fresonn commented Jun 2, 2019

Would better classes retain state while doing this 🙂?

@gaearon
Copy link
Collaborator Author

gaearon commented Jun 2, 2019

There is no reliable way to retain state for classes without subtly changing semantics. We’ve intentionally opted to not do that.

@gaearon
Copy link
Collaborator Author

gaearon commented Jun 5, 2019

Decided to remove the second feature from this PR and focus it on classes. The other feature will need more work.

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.

Seems ok to me

return type.prototype && type.prototype.isReactComponent;
}

function areCompatible(prevType, nextType) {
Copy link
Contributor

Choose a reason for hiding this comment

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

tiny nit: This name is kind of vague and threw me at first

gaearon added 5 commits June 5, 2019 12:03
This hides the implementation a little bit and reduces how much React knows about the underlying mechanism.
We already have the logic to reset a component, so let's just reuse it instead of that special case.
@gaearon gaearon merged commit d0e041a into facebook:master Jun 5, 2019
@gaearon gaearon deleted the fresh-cls branch June 5, 2019 23:13
@526117

This comment has been minimized.

@526117

This comment has been minimized.

gaearon added a commit to gaearon/react that referenced this pull request Jun 11, 2019
)

* Remount classes during hot reload

* Fix a crash when Hook isn't in scope inside the signature

* Minor tweaks

* Support a comment annotation to force state reset

* Refactoring: pass a function instead of WeakMap

This hides the implementation a little bit and reduces how much React knows about the underlying mechanism.

* Refactor: use forceReset to remount unknown Hooks

We already have the logic to reset a component, so let's just reuse it instead of that special case.
gaearon added a commit to gaearon/react that referenced this pull request Jun 19, 2019
)

* Remount classes during hot reload

* Fix a crash when Hook isn't in scope inside the signature

* Minor tweaks

* Support a comment annotation to force state reset

* Refactoring: pass a function instead of WeakMap

This hides the implementation a little bit and reduces how much React knows about the underlying mechanism.

* Refactor: use forceReset to remount unknown Hooks

We already have the logic to reset a component, so let's just reuse it instead of that special case.
rickhanlonii pushed a commit to rickhanlonii/react that referenced this pull request Jun 25, 2019
)

* Remount classes during hot reload

* Fix a crash when Hook isn't in scope inside the signature

* Minor tweaks

* Support a comment annotation to force state reset

* Refactoring: pass a function instead of WeakMap

This hides the implementation a little bit and reduces how much React knows about the underlying mechanism.

* Refactor: use forceReset to remount unknown Hooks

We already have the logic to reset a component, so let's just reuse it instead of that special case.
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.

6 participants