@@ -23,8 +23,10 @@ type FiberToLifecycleMap = Map<Fiber, LifecycleToComponentsMap>;
23
23
24
24
const ReactStrictModeWarnings = {
25
25
discardPendingWarnings ( ) : void { } ,
26
- flushPendingAsyncWarnings ( ) : void { } ,
27
- recordLifecycleWarnings ( fiber : Fiber , instance : any ) : void { } ,
26
+ flushPendingDeprecationWarnings ( ) : void { } ,
27
+ flushPendingUnsafeLifecycleWarnings ( ) : void { } ,
28
+ recordDeprecationWarnings ( fiber : Fiber , instance : any ) : void { } ,
29
+ recordUnsafeLifecycleWarnings ( fiber : Fiber , instance : any ) : void { } ,
28
30
} ;
29
31
30
32
if ( __DEV__ ) {
@@ -34,17 +36,24 @@ if (__DEV__) {
34
36
UNSAFE_componentWillUpdate : 'componentDidUpdate' ,
35
37
} ;
36
38
37
- let pendingWarningsMap : FiberToLifecycleMap = new Map ( ) ;
39
+ let pendingComponentWillMountWarnings : Array < Fiber > = [ ] ;
40
+ let pendingComponentWillReceivePropsWarnings : Array < Fiber > = [ ] ;
41
+ let pendingComponentWillUpdateWarnings : Array < Fiber > = [ ] ;
42
+ let pendingUnsafeLifecycleWarnings : FiberToLifecycleMap = new Map ( ) ;
38
43
39
44
// Tracks components we have already warned about.
40
- const didWarnSet = new Set ( ) ;
45
+ const didWarnAboutDeprecatedLifecycles = new Set ( ) ;
46
+ const didWarnAboutUnsafeLifecycles = new Set ( ) ;
41
47
42
48
ReactStrictModeWarnings . discardPendingWarnings = ( ) => {
43
- pendingWarningsMap = new Map ( ) ;
49
+ pendingComponentWillMountWarnings = [ ] ;
50
+ pendingComponentWillReceivePropsWarnings = [ ] ;
51
+ pendingComponentWillUpdateWarnings = [ ] ;
52
+ pendingUnsafeLifecycleWarnings = new Map ( ) ;
44
53
} ;
45
54
46
- ReactStrictModeWarnings . flushPendingAsyncWarnings = ( ) => {
47
- ( ( pendingWarningsMap : any ) : FiberToLifecycleMap ) . forEach (
55
+ ReactStrictModeWarnings . flushPendingUnsafeLifecycleWarnings = ( ) => {
56
+ ( ( pendingUnsafeLifecycleWarnings : any ) : FiberToLifecycleMap ) . forEach (
48
57
( lifecycleWarningsMap , strictRoot ) => {
49
58
const lifecyclesWarningMesages = [ ] ;
50
59
@@ -54,7 +63,7 @@ if (__DEV__) {
54
63
const componentNames = new Set ( ) ;
55
64
lifecycleWarnings . forEach ( fiber => {
56
65
componentNames . add ( getComponentName ( fiber ) || 'Component' ) ;
57
- didWarnSet . add ( fiber . type ) ;
66
+ didWarnAboutUnsafeLifecycles . add ( fiber . type ) ;
58
67
} ) ;
59
68
60
69
const formatted = lifecycle . replace ( 'UNSAFE_' , '' ) ;
@@ -88,7 +97,7 @@ if (__DEV__) {
88
97
} ,
89
98
) ;
90
99
91
- pendingWarningsMap = new Map ( ) ;
100
+ pendingUnsafeLifecycleWarnings = new Map ( ) ;
92
101
} ;
93
102
94
103
const getStrictRoot = ( fiber : Fiber ) : Fiber => {
@@ -105,7 +114,103 @@ if (__DEV__) {
105
114
return maybeStrictRoot ;
106
115
} ;
107
116
108
- ReactStrictModeWarnings . recordLifecycleWarnings = (
117
+ ReactStrictModeWarnings . flushPendingDeprecationWarnings = ( ) => {
118
+ if ( pendingComponentWillMountWarnings . length > 0 ) {
119
+ const uniqueNames = new Set ( ) ;
120
+ pendingComponentWillMountWarnings . forEach ( fiber => {
121
+ uniqueNames . add ( getComponentName ( fiber ) || 'Component' ) ;
122
+ didWarnAboutDeprecatedLifecycles . add ( fiber . type ) ;
123
+ } ) ;
124
+
125
+ const sortedNames = Array . from ( uniqueNames )
126
+ . sort ( )
127
+ . join ( ', ' ) ;
128
+
129
+ warning (
130
+ false ,
131
+ 'componentWillMount is deprecated and will be removed in the next major version. ' +
132
+ 'Use componentDidMount instead. As a temporary workaround, ' +
133
+ 'you can rename to UNSAFE_componentWillMount.' +
134
+ '\n\nPlease update the following components: %s' +
135
+ '\n\nLearn more about this warning here:' +
136
+ '\nhttps://fb.me/react-async-component-lifecycle-hooks' ,
137
+ sortedNames ,
138
+ ) ;
139
+
140
+ pendingComponentWillMountWarnings = [ ] ;
141
+ }
142
+
143
+ if ( pendingComponentWillReceivePropsWarnings . length > 0 ) {
144
+ const uniqueNames = new Set ( ) ;
145
+ pendingComponentWillReceivePropsWarnings . forEach ( fiber => {
146
+ uniqueNames . add ( getComponentName ( fiber ) || 'Component' ) ;
147
+ didWarnAboutDeprecatedLifecycles . add ( fiber . type ) ;
148
+ } ) ;
149
+
150
+ const sortedNames = Array . from ( uniqueNames )
151
+ . sort ( )
152
+ . join ( ', ' ) ;
153
+
154
+ warning (
155
+ false ,
156
+ 'componentWillReceiveProps is deprecated and will be removed in the next major version. ' +
157
+ 'Use static getDerivedStateFromProps instead.' +
158
+ '\n\nPlease update the following components: %s' +
159
+ '\n\nLearn more about this warning here:' +
160
+ '\nhttps://fb.me/react-async-component-lifecycle-hooks' ,
161
+ sortedNames ,
162
+ ) ;
163
+
164
+ pendingComponentWillReceivePropsWarnings = [ ] ;
165
+ }
166
+
167
+ if ( pendingComponentWillUpdateWarnings . length > 0 ) {
168
+ const uniqueNames = new Set ( ) ;
169
+ pendingComponentWillUpdateWarnings . forEach ( fiber => {
170
+ uniqueNames . add ( getComponentName ( fiber ) || 'Component' ) ;
171
+ didWarnAboutDeprecatedLifecycles . add ( fiber . type ) ;
172
+ } ) ;
173
+
174
+ const sortedNames = Array . from ( uniqueNames )
175
+ . sort ( )
176
+ . join ( ', ' ) ;
177
+
178
+ warning (
179
+ false ,
180
+ 'componentWillUpdate is deprecated and will be removed in the next major version. ' +
181
+ 'Use componentDidUpdate instead. As a temporary workaround, ' +
182
+ 'you can rename to UNSAFE_componentWillUpdate.' +
183
+ '\n\nPlease update the following components: %s' +
184
+ '\n\nLearn more about this warning here:' +
185
+ '\nhttps://fb.me/react-async-component-lifecycle-hooks' ,
186
+ sortedNames ,
187
+ ) ;
188
+
189
+ pendingComponentWillUpdateWarnings = [ ] ;
190
+ }
191
+ } ;
192
+
193
+ ReactStrictModeWarnings . recordDeprecationWarnings = (
194
+ fiber : Fiber ,
195
+ instance : any ,
196
+ ) => {
197
+ // Dedup strategy: Warn once per component.
198
+ if ( didWarnAboutDeprecatedLifecycles . has ( fiber . type ) ) {
199
+ return ;
200
+ }
201
+
202
+ if ( typeof instance . componentWillMount === 'function' ) {
203
+ pendingComponentWillMountWarnings . push ( fiber ) ;
204
+ }
205
+ if ( typeof instance . componentWillReceiveProps === 'function' ) {
206
+ pendingComponentWillReceivePropsWarnings . push ( fiber ) ;
207
+ }
208
+ if ( typeof instance . componentWillUpdate === 'function' ) {
209
+ pendingComponentWillUpdateWarnings . push ( fiber ) ;
210
+ }
211
+ } ;
212
+
213
+ ReactStrictModeWarnings . recordUnsafeLifecycleWarnings = (
109
214
fiber : Fiber ,
110
215
instance : any ,
111
216
) => {
@@ -116,21 +221,21 @@ if (__DEV__) {
116
221
// are often vague and are likely to collide between 3rd party libraries.
117
222
// An expand property is probably okay to use here since it's DEV-only,
118
223
// and will only be set in the event of serious warnings.
119
- if ( didWarnSet . has ( fiber . type ) ) {
224
+ if ( didWarnAboutUnsafeLifecycles . has ( fiber . type ) ) {
120
225
return ;
121
226
}
122
227
123
228
let warningsForRoot ;
124
- if ( ! pendingWarningsMap . has ( strictRoot ) ) {
229
+ if ( ! pendingUnsafeLifecycleWarnings . has ( strictRoot ) ) {
125
230
warningsForRoot = {
126
231
UNSAFE_componentWillMount : [ ] ,
127
232
UNSAFE_componentWillReceiveProps : [ ] ,
128
233
UNSAFE_componentWillUpdate : [ ] ,
129
234
} ;
130
235
131
- pendingWarningsMap . set ( strictRoot , warningsForRoot ) ;
236
+ pendingUnsafeLifecycleWarnings . set ( strictRoot , warningsForRoot ) ;
132
237
} else {
133
- warningsForRoot = pendingWarningsMap . get ( strictRoot ) ;
238
+ warningsForRoot = pendingUnsafeLifecycleWarnings . get ( strictRoot ) ;
134
239
}
135
240
136
241
const unsafeLifecycles = [ ] ;
0 commit comments