@@ -7,8 +7,9 @@ import * as ActionCreators from './redux/actionCreators';
77import { withDeprecationWarning } from './util' ;
88import LifecyclePanel from './components/LifecyclePanel' ;
99import { MConstructor , MShouldUpdate , MRender , MDidMount ,
10- MDidUpdate , MWillUnmount , MSetState , MGetDerivedState ,
11- MGetSnapshot , MWillMount , MWillReceiveProps , MWillUpdate } from './constants' ;
10+ MDidUpdate , MWillUnmount , MSetState , MGetDerivedState , MGetSnapshot ,
11+ MWillMount , MWillReceiveProps , MWillUpdate ,
12+ MUnsafeWillMount , MUnsafeWillReceiveProps , MUnsafeWillUpdate } from './constants' ;
1213
1314const instanceIdCounters = { } ;
1415
@@ -67,6 +68,13 @@ export default function traceLifecycle(ComponentToTrace) {
6768 }
6869 }
6970
71+ UNSAFE_componentWillMount ( ) { // eslint-disable-line camelcase
72+ this . props . trace ( MWillMount ) ; // trace it as 'componentWillMount' for brevity
73+ if ( super . UNSAFE_componentWillMount ) {
74+ super . UNSAFE_componentWillMount ( ) ;
75+ }
76+ }
77+
7078 static getDerivedStateFromProps ( nextProps , prevState ) {
7179 nextProps . trace ( MGetDerivedState ) ;
7280 return ComponentToTrace . getDerivedStateFromProps
@@ -95,6 +103,13 @@ export default function traceLifecycle(ComponentToTrace) {
95103 }
96104 }
97105
106+ UNSAFE_componentWillReceiveProps ( ...args ) { // eslint-disable-line camelcase
107+ this . props . trace ( MWillReceiveProps ) ; // trace it as 'componentWillReceiveProps' for brevity
108+ if ( super . UNSAFE_componentWillReceiveProps ) {
109+ super . UNSAFE_componentWillReceiveProps ( ...args ) ;
110+ }
111+ }
112+
98113 shouldComponentUpdate ( ...args ) {
99114 this . props . trace ( MShouldUpdate ) ;
100115 return super . shouldComponentUpdate
@@ -109,12 +124,19 @@ export default function traceLifecycle(ComponentToTrace) {
109124 }
110125 }
111126
127+ UNSAFE_componentWillUpdate ( ...args ) { // eslint-disable-line camelcase
128+ this . props . trace ( MWillUpdate ) ; // trace it as 'componentWillUpdate' for brevity
129+ if ( super . UNSAFE_componentWillUpdate ) {
130+ super . UNSAFE_componentWillUpdate ( ...args ) ;
131+ }
132+ }
133+
112134 render ( ) {
113135 if ( super . render ) {
114136 this . props . trace ( MRender ) ;
115137 return super . render ( ) ;
116138 }
117- return undefined ; // no super.render, this will trigger a React error
139+ return undefined ; // There's no super.render, which will trigger a React error
118140 }
119141
120142 getSnapshotBeforeUpdate ( ...args ) {
@@ -134,8 +156,7 @@ export default function traceLifecycle(ComponentToTrace) {
134156 setState ( updater , callback ) {
135157 this . props . trace ( MSetState ) ;
136158
137- // Unlike the lifecycle methods we only trace the update function and callback
138- // when they are actually defined.
159+ // Unlike the lifecycle methods we only trace the update function and callback when they are actually defined.
139160 const tracingUpdater = typeof updater !== 'function' ? updater : ( ...args ) => {
140161 this . props . trace ( MSetState + ':update fn' ) ;
141162 return updater ( ...args ) ;
@@ -184,18 +205,36 @@ export default function traceLifecycle(ComponentToTrace) {
184205 [ constants . reduxStoreKey ] : PropTypes . object
185206 }
186207
187- static displayName =
188- `traceLifecycle(${ componentToTraceName } )` ;
208+ static displayName = `traceLifecycle(${ componentToTraceName } )` ;
189209 }
190210
191211 // Removing the inappropriate methods is simpler than adding appropriate methods to prototype
192212 if ( isLegacy ) {
193213 delete TracedComponent . getDerivedStateFromProps ;
194214 delete TracedComponent . prototype . getSnapshotBeforeUpdate ;
215+
216+ // Only keep the tracer method corresponding to the implemented super method, unless neither the old or the
217+ // UNSAFE_ method is implemented, in which case we keep the UNSAFE_ method.
218+ // NOTE: This allows both the old method and the UNSAFE_ version to be traced, but this is correct, as React calls
219+ // both.
220+ const deleteOldOrUnsafe = ( method , unsafeMethod ) => {
221+ if ( ! superMethods . includes ( method ) ) {
222+ delete TracedComponent . prototype [ method ] ;
223+ } else if ( ! superMethods . includes ( unsafeMethod ) ) {
224+ delete TracedComponent . prototype [ unsafeMethod ] ;
225+ }
226+ } ;
227+
228+ deleteOldOrUnsafe ( MWillMount , MUnsafeWillMount ) ;
229+ deleteOldOrUnsafe ( MWillReceiveProps , MUnsafeWillReceiveProps ) ;
230+ deleteOldOrUnsafe ( MWillUpdate , MUnsafeWillUpdate ) ;
195231 } else {
196232 delete TracedComponent . prototype . componentWillMount ;
197233 delete TracedComponent . prototype . componentWillReceiveProps ;
198234 delete TracedComponent . prototype . componentWillUpdate ;
235+ delete TracedComponent . prototype . UNSAFE_componentWillMount ;
236+ delete TracedComponent . prototype . UNSAFE_componentWillReceiveProps ;
237+ delete TracedComponent . prototype . UNSAFE_componentWillUpdate ;
199238 }
200239
201240 return hoistStatics ( TracingComponent , ComponentToTrace ) ;
0 commit comments