@@ -529,7 +529,20 @@ class ReactShallowRenderer {
529529 this . _updater ,
530530 ) ;
531531
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+ }
533546
534547 if ( element . type . hasOwnProperty ( 'contextTypes ') ) {
535548 currentlyValidatingElement = element ;
@@ -653,10 +666,19 @@ class ReactShallowRenderer {
653666 }
654667 }
655668 }
656- this . _updateStateFromStaticLifecycle ( props ) ;
657669
658670 // 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+ }
660682
661683 let shouldUpdate = true;
662684 if (this._forcedUpdate) {
@@ -692,34 +714,14 @@ class ReactShallowRenderer {
692714 this . _instance . context = context ;
693715 this . _instance . props = props ;
694716 this . _instance . state = state ;
717+ this . _newState = null ;
695718
696719 if ( shouldUpdate ) {
697720 this . _rendered = this . _instance . render ( ) ;
698721 }
699722 // Intentionally do not call componentDidUpdate()
700723 // because DOM refs are not available.
701724 }
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- }
723725}
724726
725727let currentlyValidatingElement = null ;
0 commit comments