GeezerVison is showing github.com — Don't trigger lazy in DEV during element creation (#19871) · react/react@bc6b7b6
File treeExpand file treeCollapse file tree Open diff view settings react-reconciler/src/__tests__
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/__tests__/ReactLazy-test.internal.js +1-7Lines changed: 1 addition & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -979,13 +979,7 @@ describe('ReactLazy', () => { | 979 | 979 | }, | 980 | 980 | ); | 981 | 981 |
| 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...']); | 989 | 983 | expect(root).not.toMatchRenderedOutput(<div>AB</div>); | 990 | 984 |
| 991 | 985 | await Promise.resolve(); | |
Collapse file Copy file name to clipboardExpand all lines: packages/react/src/ReactElementValidator.js +4-1Lines changed: 4 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -211,7 +211,6 @@ function validatePropTypes(element) { | 211 | 211 | if (type === null || type === undefined || typeof type === 'string') { | 212 | 212 | return; | 213 | 213 | } | 214 | | - const name = getComponentName(type); | 215 | 214 | let propTypes; | 216 | 215 | if (typeof type === 'function') { | 217 | 216 | propTypes = type.propTypes; | @@ -227,9 +226,13 @@ function validatePropTypes(element) { | 227 | 226 | return; | 228 | 227 | } | 229 | 228 | if (propTypes) { | | 229 | + // Intentionally inside to avoid triggering lazy initializers: | | 230 | + const name = getComponentName(type); | 230 | 231 | checkPropTypes(propTypes, element.props, 'prop', name, element); | 231 | 232 | } else if (type.PropTypes !== undefined && !propTypesMisspellWarningShown) { | 232 | 233 | propTypesMisspellWarningShown = true; | | 234 | + // Intentionally inside to avoid triggering lazy initializers: | | 235 | + const name = getComponentName(type); | 233 | 236 | console.error( | 234 | 237 | 'Component %s declared `PropTypes` instead of `propTypes`. Did you misspell the property assignment?', | 235 | 238 | name || 'Unknown', | |
Collapse file Copy file name to clipboardExpand all lines: packages/react/src/__tests__/ReactElementJSX-test.js +14Lines changed: 14 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -415,4 +415,18 @@ describe('ReactElement.jsx', () => { | 415 | 415 | // TODO: an explicit expect for no warning? | 416 | 416 | ReactDOM.render(JSXRuntime.jsx(Parent, {}), container); | 417 | 417 | }); | | 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 | + }); | 418 | 432 | }); |
Collapse file Copy file name to clipboardExpand all lines: packages/react/src/__tests__/ReactElementValidator-test.internal.js +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -532,4 +532,14 @@ describe('ReactElementValidator', () => { | 532 | 532 | {withoutStack: true}, | 533 | 533 | ); | 534 | 534 | }); | | 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 | + }); | 535 | 545 | }); |
Collapse file Copy file name to clipboardExpand all lines: packages/react/src/__tests__/ReactJSXElementValidator-test.js +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -417,4 +417,14 @@ describe('ReactJSXElementValidator', () => { | 417 | 417 | withoutStack: true, | 418 | 418 | }); | 419 | 419 | }); | | 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 | + }); | 420 | 430 | }); |
Collapse file Copy file name to clipboardExpand all lines: packages/react/src/jsx/ReactJSXElementValidator.js +4-1Lines changed: 4 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -227,7 +227,6 @@ function validatePropTypes(element) { | 227 | 227 | if (type === null || type === undefined || typeof type === 'string') { | 228 | 228 | return; | 229 | 229 | } | 230 | | - const name = getComponentName(type); | 231 | 230 | let propTypes; | 232 | 231 | if (typeof type === 'function') { | 233 | 232 | propTypes = type.propTypes; | @@ -243,9 +242,13 @@ function validatePropTypes(element) { | 243 | 242 | return; | 244 | 243 | } | 245 | 244 | if (propTypes) { | | 245 | + // Intentionally inside to avoid triggering lazy initializers: | | 246 | + const name = getComponentName(type); | 246 | 247 | checkPropTypes(propTypes, element.props, 'prop', name, element); | 247 | 248 | } else if (type.PropTypes !== undefined && !propTypesMisspellWarningShown) { | 248 | 249 | propTypesMisspellWarningShown = true; | | 250 | + // Intentionally inside to avoid triggering lazy initializers: | | 251 | + const name = getComponentName(type); | 249 | 252 | console.error( | 250 | 253 | 'Component %s declared `PropTypes` instead of `propTypes`. Did you misspell the property assignment?', | 251 | 254 | name || 'Unknown', | |
|