Skip to content

Commit 1beb941

Browse files
authored
jsx(): Inline reserved prop checks (#28262)
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.
1 parent 0d11563 commit 1beb941

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

packages/react/src/ReactElementProd.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,6 @@ import {checkKeyStringCoercion} from 'shared/CheckStringCoercion';
1313

1414
import ReactCurrentOwner from './ReactCurrentOwner';
1515

16-
const RESERVED_PROPS = {
17-
key: true,
18-
ref: true,
19-
__self: true,
20-
__source: true,
21-
};
22-
2316
let specialPropKeyWarningShown,
2417
specialPropRefWarningShown,
2518
didWarnAboutStringRefs;
@@ -237,7 +230,12 @@ export function createElement(type, config, children) {
237230
for (propName in config) {
238231
if (
239232
hasOwnProperty.call(config, propName) &&
240-
!RESERVED_PROPS.hasOwnProperty(propName)
233+
// Skip over reserved prop names
234+
propName !== 'key' &&
235+
// TODO: These will no longer be reserved in the next major
236+
propName !== 'ref' &&
237+
propName !== '__self' &&
238+
propName !== '__source'
241239
) {
242240
props[propName] = config[propName];
243241
}
@@ -375,7 +373,12 @@ export function cloneElement(element, config, children) {
375373
for (propName in config) {
376374
if (
377375
hasOwnProperty.call(config, propName) &&
378-
!RESERVED_PROPS.hasOwnProperty(propName)
376+
// Skip over reserved prop names
377+
propName !== 'key' &&
378+
// TODO: These will no longer be reserved in the next major
379+
propName !== 'ref' &&
380+
propName !== '__self' &&
381+
propName !== '__source'
379382
) {
380383
if (config[propName] === undefined && defaultProps !== undefined) {
381384
// Resolve default props

packages/react/src/jsx/ReactJSXElement.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,6 @@ import {checkKeyStringCoercion} from 'shared/CheckStringCoercion';
1313

1414
const ReactCurrentOwner = ReactSharedInternals.ReactCurrentOwner;
1515

16-
const RESERVED_PROPS = {
17-
key: true,
18-
ref: true,
19-
__self: true,
20-
__source: true,
21-
};
22-
2316
let specialPropKeyWarningShown;
2417
let specialPropRefWarningShown;
2518
let didWarnAboutStringRefs;
@@ -244,7 +237,12 @@ export function jsx(type, config, maybeKey) {
244237
for (propName in config) {
245238
if (
246239
hasOwnProperty.call(config, propName) &&
247-
!RESERVED_PROPS.hasOwnProperty(propName)
240+
// Skip over reserved prop names
241+
propName !== 'key' &&
242+
// TODO: These will no longer be reserved in the next major
243+
propName !== 'ref' &&
244+
propName !== '__self' &&
245+
propName !== '__source'
248246
) {
249247
props[propName] = config[propName];
250248
}
@@ -316,7 +314,12 @@ export function jsxDEV(type, config, maybeKey, source, self) {
316314
for (propName in config) {
317315
if (
318316
hasOwnProperty.call(config, propName) &&
319-
!RESERVED_PROPS.hasOwnProperty(propName)
317+
// Skip over reserved prop names
318+
propName !== 'key' &&
319+
// TODO: These will no longer be reserved in the next major
320+
propName !== 'ref' &&
321+
propName !== '__self' &&
322+
propName !== '__source'
320323
) {
321324
props[propName] = config[propName];
322325
}

0 commit comments

Comments
 (0)