Skip to content

Commit

Permalink
Remove defaultProps support (except for classes) (#28733)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kassens committed Apr 11, 2024
1 parent bd1135f commit 47691a3
Show file tree
Hide file tree
Showing 17 changed files with 3,509 additions and 3,584 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -7,78 +7,44 @@
* @noflow
* @nolint
* @preventMunge
* @generated SignedSource<<b4bf34965feaa60392dd96103e5ba9fc>>
* @generated SignedSource<<26fbc8eee6902991682d526f8537439c>>
*/

"use strict";
var dynamicFlagsUntyped = require("ReactNativeInternalFeatureFlags"),
React = require("react"),
REACT_ELEMENT_TYPE = Symbol.for("react.element"),
REACT_FRAGMENT_TYPE = Symbol.for("react.fragment");
function formatProdErrorMessage(code) {
var url = "https://react.dev/errors/" + code;
if (1 < arguments.length) {
url += "?args[]=" + encodeURIComponent(arguments[1]);
for (var i = 2; i < arguments.length; i++)
url += "&args[]=" + encodeURIComponent(arguments[i]);
}
return (
"Minified React error #" +
code +
"; visit " +
url +
" for the full message or use the non-minified dev environment for full errors and additional helpful warnings."
);
}
var disableDefaultPropsExceptForClasses =
REACT_FRAGMENT_TYPE = Symbol.for("react.fragment"),
disableDefaultPropsExceptForClasses =
dynamicFlagsUntyped.disableDefaultPropsExceptForClasses,
ReactSharedInternals =
React.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE;
hasOwnProperty = Object.prototype.hasOwnProperty,
ReactCurrentOwner =
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner;
function jsxProd(type, config, maybeKey) {
var key = null,
var propName,
props = {},
key = null,
ref = null;
void 0 !== maybeKey && (key = "" + maybeKey);
void 0 !== config.key && (key = "" + config.key);
if (void 0 !== config.ref)
a: {
if (((ref = config.ref), "string" !== typeof ref))
if ("number" === typeof ref || "boolean" === typeof ref) ref = "" + ref;
else break a;
ref = stringRefAsCallbackRef.bind(
null,
ref,
type,
ReactSharedInternals.owner
);
}
maybeKey = {};
for (var propName in config)
"key" !== propName &&
void 0 !== config.ref && (ref = config.ref);
for (propName in config)
hasOwnProperty.call(config, propName) &&
"key" !== propName &&
"ref" !== propName &&
(maybeKey[propName] = config[propName]);
if (!disableDefaultPropsExceptForClasses && type && type.defaultProps) {
config = type.defaultProps;
for (var propName$0 in config)
void 0 === maybeKey[propName$0] &&
(maybeKey[propName$0] = config[propName$0]);
}
(props[propName] = config[propName]);
if (!disableDefaultPropsExceptForClasses && type && type.defaultProps)
for (propName in ((config = type.defaultProps), config))
void 0 === props[propName] && (props[propName] = config[propName]);
return {
$$typeof: REACT_ELEMENT_TYPE,
type: type,
key: key,
ref: ref,
props: maybeKey,
_owner: ReactSharedInternals.owner
props: props,
_owner: ReactCurrentOwner.current
};
}
function stringRefAsCallbackRef(stringRef, type, owner, value) {
if (!owner) throw Error(formatProdErrorMessage(290, stringRef));
if (1 !== owner.tag) throw Error(formatProdErrorMessage(309));
type = owner.stateNode;
if (!type) throw Error(formatProdErrorMessage(147, stringRef));
type = type.refs;
null === value ? delete type[stringRef] : (type[stringRef] = value);
}
exports.Fragment = REACT_FRAGMENT_TYPE;
exports.jsx = jsxProd;
exports.jsxs = jsxProd;
Original file line number Diff line number Diff line change
Expand Up @@ -7,78 +7,44 @@
* @noflow
* @nolint
* @preventMunge
* @generated SignedSource<<b4bf34965feaa60392dd96103e5ba9fc>>
* @generated SignedSource<<26fbc8eee6902991682d526f8537439c>>
*/

"use strict";
var dynamicFlagsUntyped = require("ReactNativeInternalFeatureFlags"),
React = require("react"),
REACT_ELEMENT_TYPE = Symbol.for("react.element"),
REACT_FRAGMENT_TYPE = Symbol.for("react.fragment");
function formatProdErrorMessage(code) {
var url = "https://react.dev/errors/" + code;
if (1 < arguments.length) {
url += "?args[]=" + encodeURIComponent(arguments[1]);
for (var i = 2; i < arguments.length; i++)
url += "&args[]=" + encodeURIComponent(arguments[i]);
}
return (
"Minified React error #" +
code +
"; visit " +
url +
" for the full message or use the non-minified dev environment for full errors and additional helpful warnings."
);
}
var disableDefaultPropsExceptForClasses =
REACT_FRAGMENT_TYPE = Symbol.for("react.fragment"),
disableDefaultPropsExceptForClasses =
dynamicFlagsUntyped.disableDefaultPropsExceptForClasses,
ReactSharedInternals =
React.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE;
hasOwnProperty = Object.prototype.hasOwnProperty,
ReactCurrentOwner =
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner;
function jsxProd(type, config, maybeKey) {
var key = null,
var propName,
props = {},
key = null,
ref = null;
void 0 !== maybeKey && (key = "" + maybeKey);
void 0 !== config.key && (key = "" + config.key);
if (void 0 !== config.ref)
a: {
if (((ref = config.ref), "string" !== typeof ref))
if ("number" === typeof ref || "boolean" === typeof ref) ref = "" + ref;
else break a;
ref = stringRefAsCallbackRef.bind(
null,
ref,
type,
ReactSharedInternals.owner
);
}
maybeKey = {};
for (var propName in config)
"key" !== propName &&
void 0 !== config.ref && (ref = config.ref);
for (propName in config)
hasOwnProperty.call(config, propName) &&
"key" !== propName &&
"ref" !== propName &&
(maybeKey[propName] = config[propName]);
if (!disableDefaultPropsExceptForClasses && type && type.defaultProps) {
config = type.defaultProps;
for (var propName$0 in config)
void 0 === maybeKey[propName$0] &&
(maybeKey[propName$0] = config[propName$0]);
}
(props[propName] = config[propName]);
if (!disableDefaultPropsExceptForClasses && type && type.defaultProps)
for (propName in ((config = type.defaultProps), config))
void 0 === props[propName] && (props[propName] = config[propName]);
return {
$$typeof: REACT_ELEMENT_TYPE,
type: type,
key: key,
ref: ref,
props: maybeKey,
_owner: ReactSharedInternals.owner
props: props,
_owner: ReactCurrentOwner.current
};
}
function stringRefAsCallbackRef(stringRef, type, owner, value) {
if (!owner) throw Error(formatProdErrorMessage(290, stringRef));
if (1 !== owner.tag) throw Error(formatProdErrorMessage(309));
type = owner.stateNode;
if (!type) throw Error(formatProdErrorMessage(147, stringRef));
type = type.refs;
null === value ? delete type[stringRef] : (type[stringRef] = value);
}
exports.Fragment = REACT_FRAGMENT_TYPE;
exports.jsx = jsxProd;
exports.jsxs = jsxProd;
Loading

0 comments on commit 47691a3

Please sign in to comment.