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

Deprecate module pattern (factory) components #15145

Merged
merged 1 commit into from
Mar 19, 2019

Conversation

sebmarkbage
Copy link
Collaborator

This is part of reactjs/rfcs#107 but it's not the only reason we want to deprecate it.

It requires a fairly complex code path in React that is forked from how class components are initialized. It makes function components slower, adds code bloat to React and is a maintenance burden.

It was really kind of supported from the beginning. It was originally added at a time where createClass and class-like abstractions were more popular. We also hit quirks with compile-to-JS languages when we changed how our class abstractions worked. E.g. we had to make accommodations to support ScalaJS. To avoid future problems, we kept this style of class working since it's really the simplest form of constructor in JavaScript.

However, since then when we ultimately supported both ES classes (must be called with new) and arrow function components (cannot be called with new), we had to start getting more selective about which components we call with new and which we don't.

In practice, I don't think this pattern has been widely used even by compile to JS languages or class-like abstractions.

In any official capacity, its support has only been briefly noted at the bottom of the React v0.13.0 Beta 1 post.

It is currently used by at least Relay Classic so some libraries will need to upgrade.

A neat trick is that you can almost always just add:

Foo.prototype = React.Component.prototype;

This tricks React's class detection into thinking it's a class and we'll call it with new, which means that this doesn't work on arrow functions.

@sizebot
Copy link

sizebot commented Mar 19, 2019

ReactDOM: size: 0.0%, gzip: -0.0%

Details of bundled changes.

Comparing: 1e3364e...aa4d7ff

react-dom

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-dom.development.js +0.1% +0.1% 788.24 KB 789 KB 178.95 KB 179.13 KB UMD_DEV
react-dom.production.min.js 0.0% -0.0% 104.97 KB 104.97 KB 33.91 KB 33.91 KB UMD_PROD
react-dom.profiling.min.js 0.0% -0.0% 107.95 KB 107.95 KB 34.6 KB 34.6 KB UMD_PROFILING
react-dom.development.js +0.1% +0.1% 782.63 KB 783.39 KB 177.39 KB 177.57 KB NODE_DEV
react-dom.production.min.js 0.0% -0.0% 104.94 KB 104.94 KB 33.43 KB 33.43 KB NODE_PROD
react-dom.profiling.min.js 0.0% -0.0% 108.04 KB 108.04 KB 34.04 KB 34.04 KB NODE_PROFILING
ReactDOM-dev.js +0.1% +0.1% 805.66 KB 806.53 KB 178.69 KB 178.84 KB FB_WWW_DEV
ReactDOM-profiling.js 0.0% -0.0% 324.73 KB 324.73 KB 60.17 KB 60.17 KB FB_WWW_PROFILING
react-dom-unstable-fire.development.js +0.1% +0.1% 788.58 KB 789.34 KB 179.09 KB 179.28 KB UMD_DEV
react-dom-unstable-fire.production.min.js 0.0% -0.0% 104.99 KB 104.99 KB 33.92 KB 33.92 KB UMD_PROD
react-dom-unstable-fire.profiling.min.js 0.0% -0.0% 107.96 KB 107.96 KB 34.61 KB 34.6 KB UMD_PROFILING
react-dom-unstable-fire.development.js +0.1% +0.1% 782.97 KB 783.73 KB 177.52 KB 177.71 KB NODE_DEV
react-dom-unstable-fire.production.min.js 0.0% -0.0% 104.96 KB 104.96 KB 33.44 KB 33.44 KB NODE_PROD
react-dom-unstable-fire.profiling.min.js 0.0% -0.0% 108.06 KB 108.06 KB 34.05 KB 34.04 KB NODE_PROFILING
ReactFire-dev.js +0.1% +0.1% 804.87 KB 805.74 KB 178.58 KB 178.74 KB FB_WWW_DEV
ReactFire-profiling.js 0.0% -0.0% 313.37 KB 313.37 KB 57.82 KB 57.81 KB FB_WWW_PROFILING
react-dom-test-utils.development.js 0.0% -0.0% 47.45 KB 47.45 KB 12.97 KB 12.97 KB UMD_DEV
react-dom-test-utils.production.min.js 0.0% -0.1% 9.95 KB 9.95 KB 3.66 KB 3.65 KB UMD_PROD
react-dom-test-utils.development.js 0.0% -0.0% 47.17 KB 47.17 KB 12.9 KB 12.9 KB NODE_DEV
react-dom-test-utils.production.min.js 0.0% -0.1% 9.73 KB 9.73 KB 3.59 KB 3.58 KB NODE_PROD
react-dom-unstable-native-dependencies.development.js 0.0% -0.0% 60.53 KB 60.53 KB 15.8 KB 15.8 KB UMD_DEV
react-dom-unstable-native-dependencies.production.min.js 0.0% -0.1% 10.69 KB 10.69 KB 3.67 KB 3.67 KB UMD_PROD
react-dom-unstable-native-dependencies.development.js 0.0% -0.0% 60.2 KB 60.2 KB 15.67 KB 15.67 KB NODE_DEV
react-dom-unstable-native-dependencies.production.min.js 0.0% -0.1% 10.42 KB 10.42 KB 3.57 KB 3.56 KB NODE_PROD
react-dom-server.browser.development.js +0.6% +0.4% 131.85 KB 132.58 KB 34.89 KB 35.05 KB UMD_DEV
react-dom-server.browser.production.min.js 0.0% -0.0% 18.93 KB 18.93 KB 7.14 KB 7.14 KB UMD_PROD
react-dom-server.browser.development.js +0.6% +0.5% 127.98 KB 128.71 KB 33.97 KB 34.12 KB NODE_DEV
ReactDOMServer-dev.js +0.7% +0.5% 130 KB 130.86 KB 33.63 KB 33.8 KB FB_WWW_DEV
ReactDOMServer-prod.js 0.0% -0.0% 45.67 KB 45.67 KB 10.57 KB 10.56 KB FB_WWW_PROD
react-dom-server.node.development.js +0.6% +0.4% 129.92 KB 130.65 KB 34.51 KB 34.66 KB NODE_DEV
react-dom-server.node.production.min.js 0.0% -0.0% 19.72 KB 19.72 KB 7.44 KB 7.44 KB NODE_PROD
react-dom-unstable-fizz.browser.development.js 0.0% -0.1% 3.63 KB 3.63 KB 1.44 KB 1.44 KB UMD_DEV
react-dom-unstable-fizz.browser.production.min.js 0.0% -0.1% 1.21 KB 1.21 KB 706 B 705 B UMD_PROD
react-dom-unstable-fizz.browser.development.js 0.0% -0.1% 3.45 KB 3.45 KB 1.39 KB 1.39 KB NODE_DEV
react-dom-unstable-fizz.node.development.js 0.0% -0.1% 3.7 KB 3.7 KB 1.42 KB 1.42 KB NODE_DEV

react-art

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-art.development.js +0.1% +0.2% 558.31 KB 559.07 KB 120.9 KB 121.08 KB UMD_DEV
react-art.production.min.js 0.0% -0.0% 96.78 KB 96.78 KB 29.66 KB 29.66 KB UMD_PROD
react-art.development.js +0.2% +0.2% 489.21 KB 489.97 KB 103.58 KB 103.77 KB NODE_DEV
react-art.production.min.js 0.0% -0.0% 61.76 KB 61.76 KB 18.87 KB 18.87 KB NODE_PROD
ReactART-dev.js +0.2% +0.2% 498.55 KB 499.41 KB 102.85 KB 103.01 KB FB_WWW_DEV
ReactART-prod.js 0.0% 0.0% 194.53 KB 194.53 KB 33.02 KB 33.02 KB FB_WWW_PROD

react-native-renderer

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
ReactNativeRenderer-dev.js +0.1% +0.1% 618.33 KB 619.19 KB 132.4 KB 132.56 KB RN_FB_DEV
ReactNativeRenderer-prod.js 0.0% -0.0% 245.59 KB 245.59 KB 42.92 KB 42.92 KB RN_FB_PROD
ReactNativeRenderer-profiling.js 0.0% -0.0% 251.82 KB 251.82 KB 44.27 KB 44.26 KB RN_FB_PROFILING
ReactNativeRenderer-dev.js +0.1% +0.1% 618.24 KB 619.11 KB 132.36 KB 132.53 KB RN_OSS_DEV
ReactNativeRenderer-profiling.js 0.0% -0.0% 251.84 KB 251.84 KB 44.26 KB 44.26 KB RN_OSS_PROFILING
ReactFabric-dev.js +0.1% +0.1% 608.75 KB 609.62 KB 130.08 KB 130.24 KB RN_FB_DEV
ReactFabric-prod.js 0.0% -0.0% 239.66 KB 239.66 KB 41.71 KB 41.71 KB RN_FB_PROD
ReactFabric-profiling.js 0.0% -0.0% 244.98 KB 244.98 KB 43.08 KB 43.07 KB RN_FB_PROFILING
ReactFabric-dev.js +0.1% +0.1% 608.66 KB 609.53 KB 130.03 KB 130.2 KB RN_OSS_DEV
ReactFabric-prod.js 0.0% -0.0% 239.67 KB 239.67 KB 41.71 KB 41.71 KB RN_OSS_PROD
ReactFabric-profiling.js 0.0% -0.0% 244.99 KB 244.99 KB 43.07 KB 43.07 KB RN_OSS_PROFILING

react-test-renderer

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-test-renderer.development.js +0.2% +0.2% 498.13 KB 498.89 KB 105.17 KB 105.37 KB UMD_DEV
react-test-renderer.production.min.js 0.0% -0.0% 62.71 KB 62.71 KB 19.09 KB 19.08 KB UMD_PROD
react-test-renderer.development.js +0.2% +0.2% 493.65 KB 494.41 KB 104.02 KB 104.21 KB NODE_DEV
react-test-renderer.production.min.js 0.0% -0.0% 62.39 KB 62.39 KB 18.93 KB 18.92 KB NODE_PROD
ReactTestRenderer-dev.js +0.2% +0.2% 503.95 KB 504.82 KB 103.8 KB 103.96 KB FB_WWW_DEV
react-test-renderer-shallow.development.js 0.0% -0.0% 38.95 KB 38.95 KB 9.8 KB 9.79 KB UMD_DEV
react-test-renderer-shallow.production.min.js 0.0% -0.1% 11.42 KB 11.42 KB 3.51 KB 3.5 KB UMD_PROD
react-test-renderer-shallow.development.js 0.0% -0.0% 33.18 KB 33.18 KB 8.4 KB 8.4 KB NODE_DEV
react-test-renderer-shallow.production.min.js 0.0% -0.1% 11.61 KB 11.61 KB 3.63 KB 3.63 KB NODE_PROD

react-reconciler

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-reconciler.development.js +0.2% +0.2% 487.69 KB 488.45 KB 102.17 KB 102.36 KB NODE_DEV
react-reconciler.production.min.js 0.0% -0.0% 62.89 KB 62.89 KB 18.73 KB 18.73 KB NODE_PROD
react-reconciler-persistent.development.js +0.2% +0.2% 485.67 KB 486.43 KB 101.36 KB 101.55 KB NODE_DEV
react-reconciler-persistent.production.min.js 0.0% -0.0% 62.91 KB 62.91 KB 18.73 KB 18.73 KB NODE_PROD
react-reconciler-reflection.development.js 0.0% -0.0% 15.76 KB 15.76 KB 4.9 KB 4.9 KB NODE_DEV
react-reconciler-reflection.production.min.js 0.0% -0.2% 2.37 KB 2.37 KB 1.07 KB 1.07 KB NODE_PROD

Generated by 🚫 dangerJS

@edvinerikson
Copy link
Contributor

The Relay Classic reference seems to be used in Relay Modern, but that's the only case in OSS that I've seen using this pattern.

@sebmarkbage
Copy link
Collaborator Author

I have a diff open internally to fix the Relay caller.

@sebmarkbage sebmarkbage merged commit acd65db into facebook:master Mar 19, 2019
facebook-github-bot pushed a commit to facebook/relay that referenced this pull request Mar 19, 2019
Summary:
The factory component pattern is about to be deprecate in:

facebook/react#15145

This change makes it so that React will call this constructor with `new` as if
it is a class component instead of just calling it as a function component.

There might be a slight perf penalty to doing this my understanding is that this
is not the preferred path anyway.

Reviewed By: kassens

Differential Revision: D14516681

fbshipit-source-id: 751890a98639687b208633f2e4c8e71e5df60776
@gaearon gaearon mentioned this pull request Jul 30, 2019
This was referenced Mar 10, 2020
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.

5 participants