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.
Andrew Clark committed
Feb 6, 2024 at 18:56 UTC
1beb94133a93a433669a893aef02dd5afec07394
2 files changed
+24
-18
packages/react/src/ReactElementProd.js
+12
-9
@@ -13,13 +13,6 @@ import {checkKeyStringCoercion} from 'shared/CheckStringCoercion';
13
14
import ReactCurrentOwner from './ReactCurrentOwner';
15
16
-const RESERVED_PROPS = {
17
- key: true,
18
- ref: true,
19
- __self: true,
20
- __source: true,
21
-};
22
-
16
let specialPropKeyWarningShown,
17
specialPropRefWarningShown,
18
didWarnAboutStringRefs;
@@ -237,7 +230,12 @@ export function createElement(type, config, children) {
230
for (propName in config) {
231
if (
232
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'
239
) {
240
props[propName] = config[propName];
241
}
@@ -375,7 +373,12 @@ export function cloneElement(element, config, children) {
373
for (propName in config) {
374
if (
375
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'
382
) {
383
if (config[propName] === undefined && defaultProps !== undefined) {
384
// Resolve default props
packages/react/src/jsx/ReactJSXElement.js
+12
-9
@@ -13,13 +13,6 @@ import {checkKeyStringCoercion} from 'shared/CheckStringCoercion';
13
14
const ReactCurrentOwner = ReactSharedInternals.ReactCurrentOwner;
15
16
-const RESERVED_PROPS = {
17
- key: true,
18
- ref: true,
19
- __self: true,
20
- __source: true,
21
-};
22
-
16
let specialPropKeyWarningShown;
17
let specialPropRefWarningShown;
18
let didWarnAboutStringRefs;
@@ -244,7 +237,12 @@ export function jsx(type, config, maybeKey) {
237
for (propName in config) {
238
if (
239
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'
246
) {
247
props[propName] = config[propName];
248
}
@@ -316,7 +314,12 @@ export function jsxDEV(type, config, maybeKey, source, self) {
314
for (propName in config) {
315
if (
316
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'
323
) {
324
props[propName] = config[propName];
325
}