Skip to content

Commit bc6b7b6

Browse files
authored
Don't trigger lazy in DEV during element creation (#19871)
1 parent a774502 commit bc6b7b6

6 files changed

+43
-9
lines changed

packages/react-reconciler/src/__tests__/ReactLazy-test.internal.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -979,13 +979,7 @@ describe('ReactLazy', () => {
979979
},
980980
);
981981

982-
if (__DEV__) {
983-
// Getting the name for the warning cause the loading to start early.
984-
expect(Scheduler).toHaveYielded(['Started loading']);
985-
expect(Scheduler).toFlushAndYield(['Loading...']);
986-
} else {
987-
expect(Scheduler).toFlushAndYield(['Started loading', 'Loading...']);
988-
}
982+
expect(Scheduler).toFlushAndYield(['Started loading', 'Loading...']);
989983
expect(root).not.toMatchRenderedOutput(<div>AB</div>);
990984

991985
await Promise.resolve();

packages/react/src/ReactElementValidator.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ function validatePropTypes(element) {
211211
if (type === null || type === undefined || typeof type === 'string') {
212212
return;
213213
}
214-
const name = getComponentName(type);
215214
let propTypes;
216215
if (typeof type === 'function') {
217216
propTypes = type.propTypes;
@@ -227,9 +226,13 @@ function validatePropTypes(element) {
227226
return;
228227
}
229228
if (propTypes) {
229+
// Intentionally inside to avoid triggering lazy initializers:
230+
const name = getComponentName(type);
230231
checkPropTypes(propTypes, element.props, 'prop', name, element);
231232
} else if (type.PropTypes !== undefined && !propTypesMisspellWarningShown) {
232233
propTypesMisspellWarningShown = true;
234+
// Intentionally inside to avoid triggering lazy initializers:
235+
const name = getComponentName(type);
233236
console.error(
234237
'Component %s declared `PropTypes` instead of `propTypes`. Did you misspell the property assignment?',
235238
name || 'Unknown',

packages/react/src/__tests__/ReactElementJSX-test.js

+14
Original file line numberDiff line numberDiff line change
@@ -415,4 +415,18 @@ describe('ReactElement.jsx', () => {
415415
// TODO: an explicit expect for no warning?
416416
ReactDOM.render(JSXRuntime.jsx(Parent, {}), container);
417417
});
418+
419+
it('does not call lazy initializers eagerly', () => {
420+
let didCall = false;
421+
const Lazy = React.lazy(() => {
422+
didCall = true;
423+
return {then() {}};
424+
});
425+
if (__DEV__) {
426+
JSXDEVRuntime.jsxDEV(Lazy, {});
427+
} else {
428+
JSXRuntime.jsx(Lazy, {});
429+
}
430+
expect(didCall).toBe(false);
431+
});
418432
});

packages/react/src/__tests__/ReactElementValidator-test.internal.js

+10
Original file line numberDiff line numberDiff line change
@@ -532,4 +532,14 @@ describe('ReactElementValidator', () => {
532532
{withoutStack: true},
533533
);
534534
});
535+
536+
it('does not call lazy initializers eagerly', () => {
537+
let didCall = false;
538+
const Lazy = React.lazy(() => {
539+
didCall = true;
540+
return {then() {}};
541+
});
542+
React.createElement(Lazy);
543+
expect(didCall).toBe(false);
544+
});
535545
});

packages/react/src/__tests__/ReactJSXElementValidator-test.js

+10
Original file line numberDiff line numberDiff line change
@@ -417,4 +417,14 @@ describe('ReactJSXElementValidator', () => {
417417
withoutStack: true,
418418
});
419419
});
420+
421+
it('does not call lazy initializers eagerly', () => {
422+
let didCall = false;
423+
const Lazy = React.lazy(() => {
424+
didCall = true;
425+
return {then() {}};
426+
});
427+
<Lazy />;
428+
expect(didCall).toBe(false);
429+
});
420430
});

packages/react/src/jsx/ReactJSXElementValidator.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,6 @@ function validatePropTypes(element) {
227227
if (type === null || type === undefined || typeof type === 'string') {
228228
return;
229229
}
230-
const name = getComponentName(type);
231230
let propTypes;
232231
if (typeof type === 'function') {
233232
propTypes = type.propTypes;
@@ -243,9 +242,13 @@ function validatePropTypes(element) {
243242
return;
244243
}
245244
if (propTypes) {
245+
// Intentionally inside to avoid triggering lazy initializers:
246+
const name = getComponentName(type);
246247
checkPropTypes(propTypes, element.props, 'prop', name, element);
247248
} else if (type.PropTypes !== undefined && !propTypesMisspellWarningShown) {
248249
propTypesMisspellWarningShown = true;
250+
// Intentionally inside to avoid triggering lazy initializers:
251+
const name = getComponentName(type);
249252
console.error(
250253
'Component %s declared `PropTypes` instead of `propTypes`. Did you misspell the property assignment?',
251254
name || 'Unknown',

0 commit comments

Comments
 (0)