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

Remove defaultProps support (except for classes) #28733

Merged
merged 2 commits into from
Apr 4, 2024

Conversation

acdlite
Copy link
Collaborator

@acdlite acdlite commented Apr 3, 2024

This removes defaultProps support for all component types except for classes. We've chosen to continue supporting defaultProps for classes because lots of older code relies on it, and unlike function components, (which can use default params), there's no straightforward alternative.

By implication, it also removes support for setting defaultProps on React.lazy wrapper. So this will not work:

const MyClassComponent = React.lazy(() => import('./MyClassComponent'));
// MyClassComponent is not actually a class; it's a lazy wrapper. So
// defaultProps does not work.
MyClassComponent.defaultProps = { foo: 'bar' };

However, if you set the default props on the class itself, then it's fine.

For classes, this change also moves where defaultProps are resolved. Previously, defaultProps were resolved by the JSX runtime. This change is only observable if you introspect a JSX element, which is relatively rare but does happen.

In other words, previously <ClassWithDefaultProp />.props.aDefaultProp would resolve to the default prop value, but now it does not.

@acdlite acdlite requested a review from sebmarkbage April 3, 2024 17:03
@facebook-github-bot facebook-github-bot added CLA Signed React Core Team Opened by a member of the React Core Team labels Apr 3, 2024
});
},
},
propTypes: {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Unrelated, but I removed this warning entirely since propTypes support has already been removed from the codebase.

@react-sizebot
Copy link

react-sizebot commented Apr 3, 2024

Comparing: 3761acb...41b4a69

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 = 169.31 kB 168.78 kB = 52.76 kB 52.62 kB
oss-experimental/react-dom/cjs/react-dom.production.min.js = 171.13 kB 170.60 kB = 53.33 kB 53.19 kB
facebook-www/ReactDOM-prod.classic.js +0.20% 589.03 kB 590.18 kB +0.14% 103.68 kB 103.83 kB
facebook-www/ReactDOM-prod.modern.js +0.20% 566.05 kB 567.18 kB +0.16% 99.59 kB 99.75 kB
facebook-react-native/react/cjs/JSXRuntime-prod.js +14.57% 1.43 kB 1.64 kB +10.20% 0.70 kB 0.77 kB
facebook-react-native/react/cjs/JSXRuntime-profiling.js +14.57% 1.43 kB 1.64 kB +10.20% 0.70 kB 0.77 kB
oss-experimental/react/cjs/react.react-server.development.js = 81.94 kB 80.18 kB = 22.90 kB 22.59 kB
oss-stable/react/cjs/react.react-server.development.js = 76.33 kB 74.57 kB = 21.33 kB 21.01 kB
oss-stable-semver/react/cjs/react.react-server.development.js = 76.31 kB 74.54 kB = 21.30 kB 20.98 kB
oss-experimental/react/cjs/react-jsx-runtime.react-server.production.js = 4.93 kB 4.67 kB = 2.16 kB 2.09 kB
oss-stable-semver/react/cjs/react-jsx-runtime.react-server.production.js = 4.93 kB 4.67 kB = 2.16 kB 2.09 kB
oss-stable/react/cjs/react-jsx-runtime.react-server.production.js = 4.93 kB 4.67 kB = 2.16 kB 2.09 kB
oss-experimental/react/cjs/react-jsx-runtime.production.js = 4.86 kB 4.61 kB = 2.14 kB 2.07 kB
oss-stable-semver/react/cjs/react-jsx-runtime.production.js = 4.86 kB 4.61 kB = 2.14 kB 2.07 kB
oss-stable/react/cjs/react-jsx-runtime.production.js = 4.86 kB 4.61 kB = 2.14 kB 2.07 kB
oss-experimental/react/cjs/react-jsx-runtime.profiling.js = 4.86 kB 4.61 kB = 2.14 kB 2.07 kB
oss-stable-semver/react/cjs/react-jsx-runtime.profiling.js = 4.86 kB 4.61 kB = 2.14 kB 2.07 kB
oss-stable/react/cjs/react-jsx-runtime.profiling.js = 4.86 kB 4.61 kB = 2.14 kB 2.07 kB
oss-experimental/react/cjs/react-jsx-runtime.react-server.production.min.js = 0.89 kB 0.81 kB = 0.56 kB 0.52 kB
oss-stable-semver/react/cjs/react-jsx-runtime.react-server.production.min.js = 0.89 kB 0.81 kB = 0.56 kB 0.52 kB
oss-stable/react/cjs/react-jsx-runtime.react-server.production.min.js = 0.89 kB 0.81 kB = 0.56 kB 0.52 kB
oss-experimental/react/cjs/react-jsx-runtime.production.min.js = 0.84 kB 0.76 kB = 0.54 kB 0.51 kB
oss-stable-semver/react/cjs/react-jsx-runtime.production.min.js = 0.84 kB 0.76 kB = 0.54 kB 0.51 kB
oss-stable/react/cjs/react-jsx-runtime.production.min.js = 0.84 kB 0.76 kB = 0.54 kB 0.51 kB
oss-experimental/react/cjs/react-jsx-runtime.profiling.min.js = 0.84 kB 0.76 kB = 0.54 kB 0.51 kB
oss-stable-semver/react/cjs/react-jsx-runtime.profiling.min.js = 0.84 kB 0.76 kB = 0.54 kB 0.51 kB
oss-stable/react/cjs/react-jsx-runtime.profiling.min.js = 0.84 kB 0.76 kB = 0.54 kB 0.51 kB
test_utils/ReactAllWarnings.js Deleted 64.60 kB 0.00 kB Deleted 16.14 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-react-native/react/cjs/JSXRuntime-prod.js +14.57% 1.43 kB 1.64 kB +10.20% 0.70 kB 0.77 kB
facebook-react-native/react/cjs/JSXRuntime-profiling.js +14.57% 1.43 kB 1.64 kB +10.20% 0.70 kB 0.77 kB
facebook-www/ReactServer-prod.modern.js +1.50% 17.55 kB 17.81 kB +0.96% 4.58 kB 4.63 kB
facebook-react-native/react/cjs/React-prod.js +1.28% 20.55 kB 20.81 kB +0.95% 5.16 kB 5.21 kB
facebook-www/React-prod.modern.js +1.26% 20.90 kB 21.17 kB +0.84% 5.22 kB 5.26 kB
facebook-www/React-prod.classic.js +1.26% 20.90 kB 21.17 kB +0.84% 5.22 kB 5.27 kB
facebook-react-native/react/cjs/React-profiling.js +1.26% 20.98 kB 21.24 kB +0.90% 5.23 kB 5.28 kB
facebook-www/React-profiling.modern.js +1.24% 21.34 kB 21.60 kB +0.79% 5.30 kB 5.34 kB
facebook-www/React-profiling.classic.js +1.24% 21.34 kB 21.60 kB +0.81% 5.30 kB 5.34 kB
facebook-react-native/react/cjs/JSXDEVRuntime-dev.js +0.39% 48.22 kB 48.40 kB +0.37% 12.76 kB 12.81 kB
facebook-www/JSXDEVRuntime-dev.modern.js +0.38% 50.54 kB 50.73 kB +0.35% 13.24 kB 13.29 kB
facebook-www/JSXDEVRuntime-dev.classic.js +0.38% 50.54 kB 50.73 kB +0.35% 13.24 kB 13.29 kB
facebook-react-native/react/cjs/JSXRuntime-dev.js +0.37% 49.63 kB 49.81 kB +0.40% 13.12 kB 13.17 kB
facebook-www/ReactART-prod.modern.js +0.32% 347.63 kB 348.75 kB +0.24% 58.91 kB 59.05 kB
facebook-www/ReactART-prod.classic.js +0.32% 365.53 kB 366.69 kB +0.26% 61.79 kB 61.96 kB
react-native/implementations/ReactFabric-prod.fb.js +0.31% 367.03 kB 368.19 kB +0.23% 64.27 kB 64.42 kB
react-native/implementations/ReactNativeRenderer-prod.fb.js +0.31% 374.42 kB 375.58 kB +0.32% 65.55 kB 65.75 kB
react-native/implementations/ReactFabric-profiling.fb.js +0.29% 394.26 kB 395.42 kB +0.23% 68.47 kB 68.63 kB
react-native/implementations/ReactNativeRenderer-profiling.fb.js +0.29% 401.73 kB 402.88 kB +0.23% 69.79 kB 69.95 kB
oss-stable-semver/react-server/cjs/react-server.production.js +0.25% 154.61 kB 155.01 kB +0.24% 36.48 kB 36.57 kB
oss-stable/react-server/cjs/react-server.production.js +0.25% 154.61 kB 155.01 kB +0.24% 36.48 kB 36.57 kB
oss-experimental/react-server/cjs/react-server.production.js +0.24% 166.11 kB 166.51 kB +0.26% 38.41 kB 38.51 kB
facebook-www/React-dev.classic.js = 126.40 kB 126.10 kB +0.02% 29.71 kB 29.72 kB
oss-experimental/react-reconciler/cjs/react-reconciler.development.js = 921.64 kB 919.44 kB = 198.17 kB 197.78 kB
facebook-www/React-dev.modern.js = 125.83 kB 125.52 kB +0.05% 29.60 kB 29.62 kB
oss-experimental/react-art/umd/react-art.development.js = 940.49 kB 938.23 kB = 197.82 kB 197.49 kB
oss-stable/react-reconciler/cjs/react-reconciler.development.js = 912.32 kB 910.13 kB = 196.48 kB 195.96 kB
oss-stable-semver/react-reconciler/cjs/react-reconciler.development.js = 912.30 kB 910.10 kB = 196.45 kB 195.93 kB
oss-stable/react-art/umd/react-art.development.js = 930.69 kB 928.43 kB = 196.14 kB 195.80 kB
oss-stable-semver/react-art/umd/react-art.development.js = 930.66 kB 928.40 kB = 196.12 kB 195.78 kB
facebook-react-native/react/cjs/React-dev.js = 124.48 kB 124.17 kB +0.03% 29.30 kB 29.31 kB
oss-experimental/react-art/cjs/react-art.development.js = 824.56 kB 822.37 kB = 178.59 kB 178.26 kB
oss-experimental/react-test-renderer/umd/react-test-renderer.development.js = 844.94 kB 842.67 kB = 177.77 kB 177.49 kB
oss-stable/react-test-renderer/umd/react-test-renderer.development.js = 843.64 kB 841.37 kB = 177.53 kB 177.24 kB
oss-stable-semver/react-test-renderer/umd/react-test-renderer.development.js = 843.61 kB 841.35 kB = 177.47 kB 177.19 kB
oss-stable/react-art/cjs/react-art.development.js = 815.25 kB 813.05 kB = 176.93 kB 176.59 kB
oss-stable-semver/react-art/cjs/react-art.development.js = 815.22 kB 813.03 kB = 176.90 kB 176.56 kB
oss-experimental/react-test-renderer/cjs/react-test-renderer.development.js = 806.61 kB 804.41 kB = 175.79 kB 175.50 kB
oss-stable/react-test-renderer/cjs/react-test-renderer.development.js = 805.35 kB 803.15 kB = 175.54 kB 175.25 kB
oss-stable-semver/react-test-renderer/cjs/react-test-renderer.development.js = 805.33 kB 803.13 kB = 175.51 kB 175.23 kB
oss-experimental/react-dom/cjs/react-dom.profiling.min.js = 180.41 kB 179.88 kB = 55.67 kB 55.54 kB
oss-experimental/react-dom/umd/react-dom.profiling.min.js = 179.79 kB 179.26 kB = 56.11 kB 55.98 kB
oss-stable/react-dom/cjs/react-dom.profiling.min.js = 178.59 kB 178.06 kB = 55.10 kB 54.97 kB
oss-stable-semver/react-dom/cjs/react-dom.profiling.min.js = 178.51 kB 177.98 kB = 55.07 kB 54.94 kB
oss-experimental/react-dom/cjs/react-dom-unstable_testing.production.min.js = 178.06 kB 177.54 kB = 55.86 kB 55.73 kB
oss-stable/react-dom/umd/react-dom.profiling.min.js = 177.96 kB 177.44 kB = 55.55 kB 55.40 kB
oss-stable-semver/react-dom/umd/react-dom.profiling.min.js = 177.89 kB 177.36 kB = 55.52 kB 55.37 kB
oss-experimental/react-dom/cjs/react-dom.production.min.js = 171.13 kB 170.60 kB = 53.33 kB 53.19 kB
oss-experimental/react-dom/umd/react-dom.production.min.js = 170.95 kB 170.42 kB = 53.86 kB 53.70 kB
oss-stable/react-dom/cjs/react-dom.production.min.js = 169.31 kB 168.78 kB = 52.76 kB 52.62 kB
oss-stable-semver/react-dom/cjs/react-dom.production.min.js = 169.23 kB 168.70 kB = 52.74 kB 52.60 kB
oss-stable/react-dom/umd/react-dom.production.min.js = 169.13 kB 168.60 kB = 53.28 kB 53.11 kB
oss-stable-semver/react-dom/umd/react-dom.production.min.js = 169.05 kB 168.52 kB = 53.25 kB 53.09 kB
facebook-www/ReactServer-dev.modern.js = 94.92 kB 94.62 kB +0.12% 22.44 kB 22.47 kB
oss-experimental/react-art/umd/react-art.production.min.js = 133.97 kB 133.43 kB = 41.81 kB 41.69 kB
oss-stable/react-art/umd/react-art.production.min.js = 132.16 kB 131.63 kB = 41.22 kB 41.09 kB
oss-stable-semver/react-art/umd/react-art.production.min.js = 132.11 kB 131.58 kB = 41.19 kB 41.06 kB
oss-experimental/react-reconciler/cjs/react-reconciler.profiling.min.js = 120.13 kB 119.60 kB = 36.79 kB 36.62 kB
oss-stable/react-reconciler/cjs/react-reconciler.profiling.min.js = 118.35 kB 117.83 kB = 36.19 kB 36.06 kB
oss-stable-semver/react-reconciler/cjs/react-reconciler.profiling.min.js = 118.33 kB 117.80 kB = 36.16 kB 36.03 kB
oss-experimental/react-reconciler/cjs/react-reconciler.production.min.js = 111.27 kB 110.74 kB = 34.55 kB 34.39 kB
oss-stable/react-reconciler/cjs/react-reconciler.production.min.js = 109.50 kB 108.97 kB = 33.97 kB 33.84 kB
oss-stable-semver/react-reconciler/cjs/react-reconciler.production.min.js = 109.47 kB 108.94 kB = 33.95 kB 33.82 kB
oss-experimental/react-test-renderer/cjs/react-test-renderer.production.min.js = 103.05 kB 102.52 kB = 31.63 kB 31.50 kB
oss-stable/react-test-renderer/cjs/react-test-renderer.production.min.js = 102.77 kB 102.24 kB = 31.55 kB 31.42 kB
oss-experimental/react-test-renderer/umd/react-test-renderer.production.min.js = 103.32 kB 102.79 kB = 31.95 kB 31.83 kB
oss-stable-semver/react-test-renderer/cjs/react-test-renderer.production.min.js = 102.72 kB 102.19 kB = 31.52 kB 31.40 kB
oss-stable/react-test-renderer/umd/react-test-renderer.production.min.js = 103.04 kB 102.51 kB = 31.89 kB 31.75 kB
oss-stable-semver/react-test-renderer/umd/react-test-renderer.production.min.js = 102.99 kB 102.46 kB = 31.86 kB 31.72 kB
oss-experimental/react-art/cjs/react-art.production.min.js = 96.77 kB 96.24 kB = 29.76 kB 29.62 kB
oss-stable/react-art/cjs/react-art.production.min.js = 94.96 kB 94.44 kB = 29.19 kB 29.07 kB
oss-stable-semver/react-art/cjs/react-art.production.min.js = 94.91 kB 94.39 kB = 29.16 kB 29.05 kB
oss-experimental/react/cjs/react-jsx-runtime.react-server.development.js = 42.38 kB 42.11 kB = 12.74 kB 12.68 kB
oss-stable-semver/react/cjs/react-jsx-runtime.react-server.development.js = 42.38 kB 42.11 kB = 12.74 kB 12.68 kB
oss-stable/react/cjs/react-jsx-runtime.react-server.development.js = 42.38 kB 42.11 kB = 12.74 kB 12.68 kB
oss-experimental/react/cjs/react-jsx-runtime.development.js = 42.31 kB 42.04 kB = 12.72 kB 12.66 kB
oss-stable-semver/react/cjs/react-jsx-runtime.development.js = 42.31 kB 42.04 kB = 12.72 kB 12.66 kB
oss-stable/react/cjs/react-jsx-runtime.development.js = 42.31 kB 42.04 kB = 12.72 kB 12.66 kB
oss-experimental/react/cjs/react-jsx-dev-runtime.development.js = 41.08 kB 40.81 kB = 12.37 kB 12.32 kB
oss-stable-semver/react/cjs/react-jsx-dev-runtime.development.js = 41.08 kB 40.81 kB = 12.37 kB 12.32 kB
oss-stable/react/cjs/react-jsx-dev-runtime.development.js = 41.08 kB 40.81 kB = 12.37 kB 12.32 kB
oss-experimental/react/umd/react.production.min.js = 13.22 kB 13.13 kB = 5.03 kB 4.97 kB
oss-experimental/react/umd/react.profiling.min.js = 13.21 kB 13.12 kB = 5.03 kB 4.97 kB
oss-stable/react/umd/react.production.min.js = 12.62 kB 12.53 kB = 4.86 kB 4.81 kB
oss-stable/react/umd/react.profiling.min.js = 12.62 kB 12.53 kB = 4.86 kB 4.81 kB
oss-stable-semver/react/umd/react.production.min.js = 12.59 kB 12.50 kB = 4.84 kB 4.79 kB
oss-stable-semver/react/umd/react.profiling.min.js = 12.59 kB 12.50 kB = 4.84 kB 4.79 kB
oss-experimental/react/cjs/react.production.js = 40.32 kB 40.02 kB = 11.01 kB 10.96 kB
oss-experimental/react/cjs/react.react-server.production.js = 39.73 kB 39.42 kB = 11.62 kB 11.58 kB
facebook-www/ReactDOMServer-prod.classic.js = 205.94 kB 204.36 kB +0.04% 37.44 kB 37.46 kB
oss-stable/react/cjs/react.production.js = 38.66 kB 38.36 kB = 10.70 kB 10.65 kB
oss-stable-semver/react/cjs/react.production.js = 38.63 kB 38.33 kB = 10.67 kB 10.62 kB
oss-stable/react/cjs/react.react-server.production.js = 34.41 kB 34.11 kB = 10.22 kB 10.17 kB
oss-stable-semver/react/cjs/react.react-server.production.js = 34.39 kB 34.09 kB = 10.19 kB 10.14 kB
oss-experimental/react/cjs/react.react-server.production.min.js = 9.89 kB 9.80 kB = 3.92 kB 3.87 kB
oss-experimental/react/cjs/react.production.min.js = 9.66 kB 9.57 kB = 3.56 kB 3.51 kB
oss-stable/react/cjs/react.production.min.js = 9.02 kB 8.93 kB = 3.38 kB 3.34 kB
oss-stable-semver/react/cjs/react.production.min.js = 8.99 kB 8.90 kB = 3.36 kB 3.31 kB
oss-stable/react/cjs/react.react-server.production.min.js = 8.02 kB 7.93 kB = 3.31 kB 3.26 kB
oss-stable-semver/react/cjs/react.react-server.production.min.js = 8.00 kB 7.91 kB = 3.28 kB 3.23 kB
oss-experimental/react/umd/react.development.js = 122.17 kB 120.32 kB = 31.41 kB 31.08 kB
oss-stable/react/umd/react.development.js = 120.42 kB 118.57 kB = 31.04 kB 30.70 kB
oss-stable-semver/react/umd/react.development.js = 120.40 kB 118.55 kB = 31.01 kB 30.67 kB
oss-experimental/react/cjs/react.development.js = 99.59 kB 97.83 kB = 26.82 kB 26.51 kB
oss-stable/react/cjs/react.development.js = 97.92 kB 96.16 kB = 26.47 kB 26.15 kB
oss-stable-semver/react/cjs/react.development.js = 97.90 kB 96.13 kB = 26.44 kB 26.12 kB
oss-experimental/react/cjs/react.react-server.development.js = 81.94 kB 80.18 kB = 22.90 kB 22.59 kB
oss-stable/react/cjs/react.react-server.development.js = 76.33 kB 74.57 kB = 21.33 kB 21.01 kB
oss-stable-semver/react/cjs/react.react-server.development.js = 76.31 kB 74.54 kB = 21.30 kB 20.98 kB
oss-experimental/react/cjs/react-jsx-runtime.react-server.production.js = 4.93 kB 4.67 kB = 2.16 kB 2.09 kB
oss-stable-semver/react/cjs/react-jsx-runtime.react-server.production.js = 4.93 kB 4.67 kB = 2.16 kB 2.09 kB
oss-stable/react/cjs/react-jsx-runtime.react-server.production.js = 4.93 kB 4.67 kB = 2.16 kB 2.09 kB
oss-experimental/react/cjs/react-jsx-runtime.production.js = 4.86 kB 4.61 kB = 2.14 kB 2.07 kB
oss-stable-semver/react/cjs/react-jsx-runtime.production.js = 4.86 kB 4.61 kB = 2.14 kB 2.07 kB
oss-stable/react/cjs/react-jsx-runtime.production.js = 4.86 kB 4.61 kB = 2.14 kB 2.07 kB
oss-experimental/react/cjs/react-jsx-runtime.profiling.js = 4.86 kB 4.61 kB = 2.14 kB 2.07 kB
oss-stable-semver/react/cjs/react-jsx-runtime.profiling.js = 4.86 kB 4.61 kB = 2.14 kB 2.07 kB
oss-stable/react/cjs/react-jsx-runtime.profiling.js = 4.86 kB 4.61 kB = 2.14 kB 2.07 kB
oss-experimental/react/cjs/react-jsx-runtime.react-server.production.min.js = 0.89 kB 0.81 kB = 0.56 kB 0.52 kB
oss-stable-semver/react/cjs/react-jsx-runtime.react-server.production.min.js = 0.89 kB 0.81 kB = 0.56 kB 0.52 kB
oss-stable/react/cjs/react-jsx-runtime.react-server.production.min.js = 0.89 kB 0.81 kB = 0.56 kB 0.52 kB
oss-experimental/react/cjs/react-jsx-runtime.production.min.js = 0.84 kB 0.76 kB = 0.54 kB 0.51 kB
oss-stable-semver/react/cjs/react-jsx-runtime.production.min.js = 0.84 kB 0.76 kB = 0.54 kB 0.51 kB
oss-stable/react/cjs/react-jsx-runtime.production.min.js = 0.84 kB 0.76 kB = 0.54 kB 0.51 kB
oss-experimental/react/cjs/react-jsx-runtime.profiling.min.js = 0.84 kB 0.76 kB = 0.54 kB 0.51 kB
oss-stable-semver/react/cjs/react-jsx-runtime.profiling.min.js = 0.84 kB 0.76 kB = 0.54 kB 0.51 kB
oss-stable/react/cjs/react-jsx-runtime.profiling.min.js = 0.84 kB 0.76 kB = 0.54 kB 0.51 kB
test_utils/ReactAllWarnings.js Deleted 64.60 kB 0.00 kB Deleted 16.14 kB 0.00 kB

Generated by 🚫 dangerJS against 41b4a69

This removes defaultProps support for all component types except for
classes. We've chosen to continue supporting defaultProps for classes
because lots of older code relies on it, and unlike function components,
(which can use default params), there's no straightforward alternative.

By implication, it also removes support for setting defaultProps on
`React.lazy` wrapper. So this will not work:

```js
const MyClassComponent = React.lazy(() => import('./MyClassComponent'));
// MyClassComponent is not actually a class; it's a lazy wrapper. So
// defaultProps does not work.
MyClassComponent.defaultProps = { foo: 'bar' };
```

However, if you set the default props on the class itself, then
it's fine.

For classes, this change also moves where defaultProps are resolved.
Previously, defaultProps were resolved by the JSX runtime. This change
is only observable if you introspect a JSX element, which is relatively
rare but does happen.

In other words, previously `<ClassWithDefaultProp />.props.aDefaultProp`
would resolve to the default prop value, but now it does not.
@acdlite acdlite force-pushed the remove-defaultprops-support branch from 44a5b6c to 41b4a69 Compare April 3, 2024 17:41
if (props[propName] === undefined) {
props[propName] = defaultProps[propName];
if (!disableDefaultPropsExceptForClasses) {
// Resolve default props
Copy link
Collaborator

Choose a reason for hiding this comment

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

Shouldn't this be removed even when the flag is off? Because the resolution moves regardless?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The way I implemented the flag is that when it's off, it maintains the old behavior: props resolved during JSX. I did it that way to aid the roll out at Meta and reduce the risk the whole change gets reverted. Since there could be code that reads element.props and expects the defaultProps to already be there.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

So really the flag handles both moving the resolution and removing it for non-classes, I just didn't think it was worth creating two separate flags.

@acdlite acdlite merged commit 48b4ecc into facebook:main Apr 4, 2024
38 checks passed
github-actions bot pushed a commit that referenced this pull request Apr 4, 2024
This removes defaultProps support for all component types except for
classes. We've chosen to continue supporting defaultProps for classes
because lots of older code relies on it, and unlike function components,
(which can use default params), there's no straightforward alternative.

By implication, it also removes support for setting defaultProps on
`React.lazy` wrapper. So this will not work:

```js
const MyClassComponent = React.lazy(() => import('./MyClassComponent'));
// MyClassComponent is not actually a class; it's a lazy wrapper. So
// defaultProps does not work.
MyClassComponent.defaultProps = { foo: 'bar' };
```

However, if you set the default props on the class itself, then it's
fine.

For classes, this change also moves where defaultProps are resolved.
Previously, defaultProps were resolved by the JSX runtime. This change
is only observable if you introspect a JSX element, which is relatively
rare but does happen.

In other words, previously `<ClassWithDefaultProp />.props.aDefaultProp`
would resolve to the default prop value, but now it does not.

DiffTrain build for [48b4ecc](48b4ecc)
github-actions bot pushed a commit that referenced this pull request Apr 11, 2024
This removes defaultProps support for all component types except for
classes. We've chosen to continue supporting defaultProps for classes
because lots of older code relies on it, and unlike function components,
(which can use default params), there's no straightforward alternative.

By implication, it also removes support for setting defaultProps on
`React.lazy` wrapper. So this will not work:

```js
const MyClassComponent = React.lazy(() => import('./MyClassComponent'));
// MyClassComponent is not actually a class; it's a lazy wrapper. So
// defaultProps does not work.
MyClassComponent.defaultProps = { foo: 'bar' };
```

However, if you set the default props on the class itself, then it's
fine.

For classes, this change also moves where defaultProps are resolved.
Previously, defaultProps were resolved by the JSX runtime. This change
is only observable if you introspect a JSX element, which is relatively
rare but does happen.

In other words, previously `<ClassWithDefaultProp />.props.aDefaultProp`
would resolve to the default prop value, but now it does not.

DiffTrain build for [48b4ecc](48b4ecc)
github-actions bot pushed a commit that referenced this pull request Apr 11, 2024
This removes defaultProps support for all component types except for
classes. We've chosen to continue supporting defaultProps for classes
because lots of older code relies on it, and unlike function components,
(which can use default params), there's no straightforward alternative.

By implication, it also removes support for setting defaultProps on
`React.lazy` wrapper. So this will not work:

```js
const MyClassComponent = React.lazy(() => import('./MyClassComponent'));
// MyClassComponent is not actually a class; it's a lazy wrapper. So
// defaultProps does not work.
MyClassComponent.defaultProps = { foo: 'bar' };
```

However, if you set the default props on the class itself, then it's
fine.

For classes, this change also moves where defaultProps are resolved.
Previously, defaultProps were resolved by the JSX runtime. This change
is only observable if you introspect a JSX element, which is relatively
rare but does happen.

In other words, previously `<ClassWithDefaultProp />.props.aDefaultProp`
would resolve to the default prop value, but now it does not.

DiffTrain build for [48b4ecc](48b4ecc)
EdisonVan pushed a commit to EdisonVan/react that referenced this pull request Apr 15, 2024
This removes defaultProps support for all component types except for
classes. We've chosen to continue supporting defaultProps for classes
because lots of older code relies on it, and unlike function components,
(which can use default params), there's no straightforward alternative.

By implication, it also removes support for setting defaultProps on
`React.lazy` wrapper. So this will not work:

```js
const MyClassComponent = React.lazy(() => import('./MyClassComponent'));
// MyClassComponent is not actually a class; it's a lazy wrapper. So
// defaultProps does not work.
MyClassComponent.defaultProps = { foo: 'bar' };
```

However, if you set the default props on the class itself, then it's
fine.

For classes, this change also moves where defaultProps are resolved.
Previously, defaultProps were resolved by the JSX runtime. This change
is only observable if you introspect a JSX element, which is relatively
rare but does happen.

In other words, previously `<ClassWithDefaultProp />.props.aDefaultProp`
would resolve to the default prop value, but now it does not.
bigfootjon pushed a commit that referenced this pull request Apr 18, 2024
This removes defaultProps support for all component types except for
classes. We've chosen to continue supporting defaultProps for classes
because lots of older code relies on it, and unlike function components,
(which can use default params), there's no straightforward alternative.

By implication, it also removes support for setting defaultProps on
`React.lazy` wrapper. So this will not work:

```js
const MyClassComponent = React.lazy(() => import('./MyClassComponent'));
// MyClassComponent is not actually a class; it's a lazy wrapper. So
// defaultProps does not work.
MyClassComponent.defaultProps = { foo: 'bar' };
```

However, if you set the default props on the class itself, then it's
fine.

For classes, this change also moves where defaultProps are resolved.
Previously, defaultProps were resolved by the JSX runtime. This change
is only observable if you introspect a JSX element, which is relatively
rare but does happen.

In other words, previously `<ClassWithDefaultProp />.props.aDefaultProp`
would resolve to the default prop value, but now it does not.

DiffTrain build for commit 48b4ecc.
bigfootjon pushed a commit that referenced this pull request Apr 18, 2024
This removes defaultProps support for all component types except for
classes. We've chosen to continue supporting defaultProps for classes
because lots of older code relies on it, and unlike function components,
(which can use default params), there's no straightforward alternative.

By implication, it also removes support for setting defaultProps on
`React.lazy` wrapper. So this will not work:

```js
const MyClassComponent = React.lazy(() => import('./MyClassComponent'));
// MyClassComponent is not actually a class; it's a lazy wrapper. So
// defaultProps does not work.
MyClassComponent.defaultProps = { foo: 'bar' };
```

However, if you set the default props on the class itself, then it's
fine.

For classes, this change also moves where defaultProps are resolved.
Previously, defaultProps were resolved by the JSX runtime. This change
is only observable if you introspect a JSX element, which is relatively
rare but does happen.

In other words, previously `<ClassWithDefaultProp />.props.aDefaultProp`
would resolve to the default prop value, but now it does not.

DiffTrain build for commit 48b4ecc.
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.

4 participants