@@ -529,7 +529,20 @@ class ReactShallowRenderer {
529
529
this . _updater ,
530
530
) ;
531
531
532
- this . _updateStateFromStaticLifecycle ( element . props ) ;
532
+ if ( typeof element . type . getDerivedStateFromProps === 'function' ) {
533
+ const partialState = element . type . getDerivedStateFromProps . call (
534
+ null ,
535
+ element . props ,
536
+ this . _instance . state ,
537
+ ) ;
538
+ if ( partialState != null ) {
539
+ this . _instance . state = Object . assign (
540
+ { } ,
541
+ this . _instance . state ,
542
+ partialState ,
543
+ ) ;
544
+ }
545
+ }
533
546
534
547
if ( element . type . hasOwnProperty ( 'contextTypes ') ) {
535
548
currentlyValidatingElement = element ;
@@ -653,10 +666,19 @@ class ReactShallowRenderer {
653
666
}
654
667
}
655
668
}
656
- this . _updateStateFromStaticLifecycle ( props ) ;
657
669
658
670
// Read state after cWRP in case it calls setState
659
- const state = this . _newState || oldState ;
671
+ let state = this . _newState || oldState ;
672
+ if ( typeof type . getDerivedStateFromProps === 'function ') {
673
+ const partialState = type . getDerivedStateFromProps . call (
674
+ null ,
675
+ props ,
676
+ state ,
677
+ ) ;
678
+ if ( partialState != null ) {
679
+ state = Object . assign ( { } , state , partialState ) ;
680
+ }
681
+ }
660
682
661
683
let shouldUpdate = true;
662
684
if (this._forcedUpdate) {
@@ -692,34 +714,14 @@ class ReactShallowRenderer {
692
714
this . _instance . context = context ;
693
715
this . _instance . props = props ;
694
716
this . _instance . state = state ;
717
+ this . _newState = null ;
695
718
696
719
if ( shouldUpdate ) {
697
720
this . _rendered = this . _instance . render ( ) ;
698
721
}
699
722
// Intentionally do not call componentDidUpdate()
700
723
// because DOM refs are not available.
701
724
}
702
-
703
- _updateStateFromStaticLifecycle ( props : Object ) {
704
- if ( this . _element === null ) {
705
- return ;
706
- }
707
- const { type } = this._element;
708
-
709
- if (typeof type.getDerivedStateFromProps === 'function') {
710
- const oldState = this . _newState || this . _instance . state ;
711
- const partialState = type . getDerivedStateFromProps . call (
712
- null ,
713
- props ,
714
- oldState ,
715
- ) ;
716
-
717
- if ( partialState != null ) {
718
- const newState = Object . assign ( { } , oldState , partialState ) ;
719
- this . _instance . state = this . _newState = newState ;
720
- }
721
- }
722
- }
723
725
}
724
726
725
727
let currentlyValidatingElement = null ;
0 commit comments