@@ -41,14 +41,14 @@ const isArray = Array.isArray;
41
41
let didWarnAboutStateAssignmentForComponent ;
42
42
let didWarnAboutUndefinedDerivedState ;
43
43
let didWarnAboutUninitializedState ;
44
- let didWarnAboutWillReceivePropsAndDerivedState ;
44
+ let didWarnAboutLegacyLifecyclesAndDerivedState ;
45
45
let warnOnInvalidCallback ;
46
46
47
47
if ( __DEV__ ) {
48
48
didWarnAboutStateAssignmentForComponent = { } ;
49
49
didWarnAboutUndefinedDerivedState = { } ;
50
50
didWarnAboutUninitializedState = { } ;
51
- didWarnAboutWillReceivePropsAndDerivedState = { } ;
51
+ didWarnAboutLegacyLifecyclesAndDerivedState = { } ;
52
52
53
53
const didWarnOnInvalidCallback = { } ;
54
54
@@ -435,20 +435,75 @@ export default function(
435
435
adoptClassInstance ( workInProgress , instance ) ;
436
436
437
437
if ( __DEV__ ) {
438
- if (
439
- typeof ctor . getDerivedStateFromProps === 'function' &&
440
- state === null
441
- ) {
442
- const componentName = getComponentName ( workInProgress ) || 'Component' ;
443
- if ( ! didWarnAboutUninitializedState [ componentName ] ) {
444
- warning (
445
- false ,
446
- '%s: Did not properly initialize state during construction. ' +
447
- 'Expected state to be an object, but it was %s.' ,
448
- componentName ,
449
- instance . state === null ? 'null' : 'undefined' ,
450
- ) ;
451
- didWarnAboutUninitializedState [ componentName ] = true ;
438
+ if ( typeof ctor . getDerivedStateFromProps === 'function' ) {
439
+ if ( state === null ) {
440
+ const componentName = getComponentName ( workInProgress ) || 'Component' ;
441
+ if ( ! didWarnAboutUninitializedState [ componentName ] ) {
442
+ warning (
443
+ false ,
444
+ '%s: Did not properly initialize state during construction. ' +
445
+ 'Expected state to be an object, but it was %s.' ,
446
+ componentName ,
447
+ instance . state === null ? 'null' : 'undefined' ,
448
+ ) ;
449
+ didWarnAboutUninitializedState [ componentName ] = true ;
450
+ }
451
+ }
452
+
453
+ // If getDerivedStateFromProps() is defined, "unsafe" lifecycles won't be called.
454
+ // Warn about these lifecycles if they are present.
455
+ // Don't warn about react-lifecycles-compat polyfilled methods though.
456
+ let foundWillMountName = null ;
457
+ let foundWillReceivePropsName = null ;
458
+ let foundWillUpdateName = null ;
459
+ if (
460
+ typeof instance . componentWillMount === 'function' &&
461
+ instance . componentWillMount . __suppressDeprecationWarning !== true
462
+ ) {
463
+ foundWillMountName = 'componentWillMount' ;
464
+ } else if ( typeof instance . UNSAFE_componentWillMount === 'function' ) {
465
+ foundWillMountName = 'UNSAFE_componentWillMount' ;
466
+ }
467
+ if (
468
+ typeof instance . componentWillReceiveProps === 'function' &&
469
+ instance . componentWillReceiveProps . __suppressDeprecationWarning !==
470
+ true
471
+ ) {
472
+ foundWillReceivePropsName = 'componentWillReceiveProps' ;
473
+ } else if (
474
+ typeof instance . UNSAFE_componentWillReceiveProps === 'function'
475
+ ) {
476
+ foundWillReceivePropsName = 'UNSAFE_componentWillReceiveProps' ;
477
+ }
478
+ if ( typeof instance . componentWillUpdate === 'function' ) {
479
+ foundWillUpdateName = 'componentWillUpdate' ;
480
+ } else if ( typeof instance . UNSAFE_componentWillUpdate === 'function' ) {
481
+ foundWillUpdateName = 'UNSAFE_componentWillUpdate' ;
482
+ }
483
+ if (
484
+ foundWillMountName !== null ||
485
+ foundWillReceivePropsName !== null ||
486
+ foundWillUpdateName !== null
487
+ ) {
488
+ const componentName = getComponentName ( workInProgress ) || 'Component' ;
489
+ if ( ! didWarnAboutLegacyLifecyclesAndDerivedState [ componentName ] ) {
490
+ warning (
491
+ false ,
492
+ 'Unsafe legacy lifecycles will not be called for components using ' +
493
+ 'the new getDerivedStateFromProps() API.\n\n' +
494
+ '%s uses getDerivedStateFromProps() but also contains the following legacy lifecycles:' +
495
+ '%s%s%s\n\n' +
496
+ 'The above lifecycles should be removed. Learn more about this warning here:\n' +
497
+ 'https://fb.me/react-async-component-lifecycle-hooks' ,
498
+ componentName ,
499
+ foundWillMountName !== null ? `\n ${ foundWillMountName } ` : '' ,
500
+ foundWillReceivePropsName !== null
501
+ ? `\n ${ foundWillReceivePropsName } `
502
+ : '' ,
503
+ foundWillUpdateName !== null ? `\n ${ foundWillUpdateName } ` : '' ,
504
+ ) ;
505
+ didWarnAboutLegacyLifecyclesAndDerivedState [ componentName ] = true ;
506
+ }
452
507
}
453
508
}
454
509
}
@@ -552,28 +607,6 @@ export default function(
552
607
const { type} = workInProgress ;
553
608
554
609
if ( typeof type . getDerivedStateFromProps === 'function' ) {
555
- if ( __DEV__ ) {
556
- // Don't warn about react-lifecycles-compat polyfilled components
557
- if (
558
- ( typeof instance . componentWillReceiveProps === 'function' &&
559
- instance . componentWillReceiveProps . __suppressDeprecationWarning !==
560
- true ) ||
561
- typeof instance . UNSAFE_componentWillReceiveProps === 'function'
562
- ) {
563
- const componentName = getComponentName ( workInProgress ) || 'Component' ;
564
- if ( ! didWarnAboutWillReceivePropsAndDerivedState [ componentName ] ) {
565
- warning (
566
- false ,
567
- '%s: Defines both componentWillReceiveProps() and static ' +
568
- 'getDerivedStateFromProps() methods. We recommend using ' +
569
- 'only getDerivedStateFromProps().' ,
570
- componentName ,
571
- ) ;
572
- didWarnAboutWillReceivePropsAndDerivedState [ componentName ] = true ;
573
- }
574
- }
575
- }
576
-
577
610
if (
578
611
debugRenderPhaseSideEffects ||
579
612
( debugRenderPhaseSideEffectsForStrictMode &&
0 commit comments