@@ -56,7 +56,9 @@ const ensureChild = child =>
5656const getChildren = element =>
5757 element . props && element . props . children
5858 ? element . props . children
59- : element . children ? element . children : undefined
59+ : element . children
60+ ? element . children
61+ : undefined
6062
6163// Preact uses "nodeName", React uses "type"
6264const getType = element => element . type || element . nodeName
@@ -192,7 +194,9 @@ export default function reactTreeWalker(
192194 const instance = new Component ( props , currentContext )
193195
194196 // In case the user doesn't pass these to super in the constructor
195- instance . props = instance . props || props
197+ Object . defineProperty ( instance , 'props' , {
198+ value : instance . props || props ,
199+ } )
196200 instance . context = instance . context || currentContext
197201 // set the instance state to null (not undefined) if not set, to match React behaviour
198202 instance . state = instance . state || null
@@ -210,7 +214,17 @@ export default function reactTreeWalker(
210214 instance . state = Object . assign ( { } , instance . state , newState )
211215 }
212216
213- if ( instance . componentWillMount ) {
217+ if ( Component . getDerivedStateFromProps ) {
218+ const result = Component . getDerivedStateFromProps (
219+ instance . props ,
220+ instance . state ,
221+ )
222+ if ( result !== null ) {
223+ instance . state = Object . assign ( { } , instance . state , result )
224+ }
225+ } else if ( instance . UNSAFE_componentWillMount ) {
226+ instance . UNSAFE_componentWillMount ( )
227+ } else if ( instance . componentWillMount ) {
214228 instance . componentWillMount ( )
215229 }
216230
0 commit comments