Skip to content

Commit

Permalink
jsx(): Inline reserved prop checks (#28262)
Browse files Browse the repository at this point in the history
The JSX runtime (both the new one and the classic createElement runtime)
check for reserved props like `key` and `ref` by doing a lookup in a
plain object map with `hasOwnProperty`.

There are only a few reserved props so this inlines the checks instead.
  • Loading branch information
acdlite committed Feb 6, 2024
1 parent 0d11563 commit 1beb941
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
21 changes: 12 additions & 9 deletions packages/react/src/ReactElementProd.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@ import {checkKeyStringCoercion} from 'shared/CheckStringCoercion';

import ReactCurrentOwner from './ReactCurrentOwner';

const RESERVED_PROPS = {
key: true,
ref: true,
__self: true,
__source: true,
};

let specialPropKeyWarningShown,
specialPropRefWarningShown,
didWarnAboutStringRefs;
Expand Down Expand Up @@ -237,7 +230,12 @@ export function createElement(type, config, children) {
for (propName in config) {
if (
hasOwnProperty.call(config, propName) &&
!RESERVED_PROPS.hasOwnProperty(propName)
// Skip over reserved prop names
propName !== 'key' &&
// TODO: These will no longer be reserved in the next major
propName !== 'ref' &&
propName !== '__self' &&
propName !== '__source'
) {
props[propName] = config[propName];
}
Expand Down Expand Up @@ -375,7 +373,12 @@ export function cloneElement(element, config, children) {
for (propName in config) {
if (
hasOwnProperty.call(config, propName) &&
!RESERVED_PROPS.hasOwnProperty(propName)
// Skip over reserved prop names
propName !== 'key' &&
// TODO: These will no longer be reserved in the next major
propName !== 'ref' &&
propName !== '__self' &&
propName !== '__source'
) {
if (config[propName] === undefined && defaultProps !== undefined) {
// Resolve default props
Expand Down
21 changes: 12 additions & 9 deletions packages/react/src/jsx/ReactJSXElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@ import {checkKeyStringCoercion} from 'shared/CheckStringCoercion';

const ReactCurrentOwner = ReactSharedInternals.ReactCurrentOwner;

const RESERVED_PROPS = {
key: true,
ref: true,
__self: true,
__source: true,
};

let specialPropKeyWarningShown;
let specialPropRefWarningShown;
let didWarnAboutStringRefs;
Expand Down Expand Up @@ -244,7 +237,12 @@ export function jsx(type, config, maybeKey) {
for (propName in config) {
if (
hasOwnProperty.call(config, propName) &&
!RESERVED_PROPS.hasOwnProperty(propName)
// Skip over reserved prop names
propName !== 'key' &&
// TODO: These will no longer be reserved in the next major
propName !== 'ref' &&
propName !== '__self' &&
propName !== '__source'
) {
props[propName] = config[propName];
}
Expand Down Expand Up @@ -316,7 +314,12 @@ export function jsxDEV(type, config, maybeKey, source, self) {
for (propName in config) {
if (
hasOwnProperty.call(config, propName) &&
!RESERVED_PROPS.hasOwnProperty(propName)
// Skip over reserved prop names
propName !== 'key' &&
// TODO: These will no longer be reserved in the next major
propName !== 'ref' &&
propName !== '__self' &&
propName !== '__source'
) {
props[propName] = config[propName];
}
Expand Down

0 comments on commit 1beb941

Please sign in to comment.