forked from facebook/react
/
ReactStrictModeWarnings.js
366 lines (317 loc) · 13.3 KB
/
ReactStrictModeWarnings.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import type {Fiber} from './ReactFiber';
import {getStackByFiberInDevAndProd} from './ReactCurrentFiber';
import getComponentName from 'shared/getComponentName';
import {StrictMode} from './ReactTypeOfMode';
type FiberArray = Array<Fiber>;
type FiberToFiberComponentsMap = Map<Fiber, FiberArray>;
const ReactStrictModeWarnings = {
recordUnsafeLifecycleWarnings(fiber: Fiber, instance: any): void {},
flushPendingUnsafeLifecycleWarnings(): void {},
recordLegacyContextWarning(fiber: Fiber, instance: any): void {},
flushLegacyContextWarning(): void {},
discardPendingWarnings(): void {},
};
if (__DEV__) {
const findStrictRoot = (fiber: Fiber): Fiber | null => {
let maybeStrictRoot = null;
let node = fiber;
while (node !== null) {
if (node.mode & StrictMode) {
maybeStrictRoot = node;
}
node = node.return;
}
return maybeStrictRoot;
};
const setToSortedString = set => {
const array = [];
set.forEach(value => {
array.push(value);
});
return array.sort().join(', ');
};
let pendingComponentWillMountWarnings: Array<Fiber> = [];
let pendingUNSAFE_ComponentWillMountWarnings: Array<Fiber> = [];
let pendingComponentWillReceivePropsWarnings: Array<Fiber> = [];
let pendingUNSAFE_ComponentWillReceivePropsWarnings: Array<Fiber> = [];
let pendingComponentWillUpdateWarnings: Array<Fiber> = [];
let pendingUNSAFE_ComponentWillUpdateWarnings: Array<Fiber> = [];
// Tracks components we have already warned about.
const didWarnAboutUnsafeLifecycles = new Set();
ReactStrictModeWarnings.recordUnsafeLifecycleWarnings = (
fiber: Fiber,
instance: any,
) => {
// Dedup strategy: Warn once per component.
if (didWarnAboutUnsafeLifecycles.has(fiber.type)) {
return;
}
if (
typeof instance.componentWillMount === 'function' &&
// Don't warn about react-lifecycles-compat polyfilled components.
instance.componentWillMount.__suppressDeprecationWarning !== true
) {
pendingComponentWillMountWarnings.push(fiber);
}
if (
fiber.mode & StrictMode &&
typeof instance.UNSAFE_componentWillMount === 'function'
) {
pendingUNSAFE_ComponentWillMountWarnings.push(fiber);
}
if (
typeof instance.componentWillReceiveProps === 'function' &&
instance.componentWillReceiveProps.__suppressDeprecationWarning !== true
) {
pendingComponentWillReceivePropsWarnings.push(fiber);
}
if (
fiber.mode & StrictMode &&
typeof instance.UNSAFE_componentWillReceiveProps === 'function'
) {
pendingUNSAFE_ComponentWillReceivePropsWarnings.push(fiber);
}
if (
typeof instance.componentWillUpdate === 'function' &&
instance.componentWillUpdate.__suppressDeprecationWarning !== true
) {
pendingComponentWillUpdateWarnings.push(fiber);
}
if (
fiber.mode & StrictMode &&
typeof instance.UNSAFE_componentWillUpdate === 'function'
) {
pendingUNSAFE_ComponentWillUpdateWarnings.push(fiber);
}
};
ReactStrictModeWarnings.flushPendingUnsafeLifecycleWarnings = () => {
// We do an initial pass to gather component names
const componentWillMountUniqueNames = new Set();
if (pendingComponentWillMountWarnings.length > 0) {
pendingComponentWillMountWarnings.forEach(fiber => {
componentWillMountUniqueNames.add(
getComponentName(fiber.type) || 'Component',
);
didWarnAboutUnsafeLifecycles.add(fiber.type);
});
pendingComponentWillMountWarnings = [];
}
const UNSAFE_componentWillMountUniqueNames = new Set();
if (pendingUNSAFE_ComponentWillMountWarnings.length > 0) {
pendingUNSAFE_ComponentWillMountWarnings.forEach(fiber => {
UNSAFE_componentWillMountUniqueNames.add(
getComponentName(fiber.type) || 'Component',
);
didWarnAboutUnsafeLifecycles.add(fiber.type);
});
pendingUNSAFE_ComponentWillMountWarnings = [];
}
const componentWillReceivePropsUniqueNames = new Set();
if (pendingComponentWillReceivePropsWarnings.length > 0) {
pendingComponentWillReceivePropsWarnings.forEach(fiber => {
componentWillReceivePropsUniqueNames.add(
getComponentName(fiber.type) || 'Component',
);
didWarnAboutUnsafeLifecycles.add(fiber.type);
});
pendingComponentWillReceivePropsWarnings = [];
}
const UNSAFE_componentWillReceivePropsUniqueNames = new Set();
if (pendingUNSAFE_ComponentWillReceivePropsWarnings.length > 0) {
pendingUNSAFE_ComponentWillReceivePropsWarnings.forEach(fiber => {
UNSAFE_componentWillReceivePropsUniqueNames.add(
getComponentName(fiber.type) || 'Component',
);
didWarnAboutUnsafeLifecycles.add(fiber.type);
});
pendingUNSAFE_ComponentWillReceivePropsWarnings = [];
}
const componentWillUpdateUniqueNames = new Set();
if (pendingComponentWillUpdateWarnings.length > 0) {
pendingComponentWillUpdateWarnings.forEach(fiber => {
componentWillUpdateUniqueNames.add(
getComponentName(fiber.type) || 'Component',
);
didWarnAboutUnsafeLifecycles.add(fiber.type);
});
pendingComponentWillUpdateWarnings = [];
}
const UNSAFE_componentWillUpdateUniqueNames = new Set();
if (pendingUNSAFE_ComponentWillUpdateWarnings.length > 0) {
pendingUNSAFE_ComponentWillUpdateWarnings.forEach(fiber => {
UNSAFE_componentWillUpdateUniqueNames.add(
getComponentName(fiber.type) || 'Component',
);
didWarnAboutUnsafeLifecycles.add(fiber.type);
});
pendingUNSAFE_ComponentWillUpdateWarnings = [];
}
// Finally, we flush all the warnings
// UNSAFE_ ones before the deprecated ones, since they'll be 'louder'
if (UNSAFE_componentWillMountUniqueNames.size > 0) {
const sortedNames = setToSortedString(
UNSAFE_componentWillMountUniqueNames,
);
console.error(
'Using UNSAFE_componentWillMount in strict mode is not recommended and may indicate bugs in your code. ' +
'See https://fb.me/react-unsafe-component-lifecycles for details.\n\n' +
'* Move code with side effects to componentDidMount, and set initial state in the constructor.\n' +
'\nPlease update the following components: %s',
sortedNames,
);
}
if (UNSAFE_componentWillReceivePropsUniqueNames.size > 0) {
const sortedNames = setToSortedString(
UNSAFE_componentWillReceivePropsUniqueNames,
);
console.error(
'Using UNSAFE_componentWillReceiveProps in strict mode is not recommended ' +
'and may indicate bugs in your code. ' +
'See https://fb.me/react-unsafe-component-lifecycles for details.\n\n' +
'* Move data fetching code or side effects to componentDidUpdate.\n' +
"* If you're updating state whenever props change, " +
'refactor your code to use memoization techniques or move it to ' +
'static getDerivedStateFromProps. Learn more at: https://fb.me/react-derived-state\n' +
'\nPlease update the following components: %s',
sortedNames,
);
}
if (UNSAFE_componentWillUpdateUniqueNames.size > 0) {
const sortedNames = setToSortedString(
UNSAFE_componentWillUpdateUniqueNames,
);
console.error(
'Using UNSAFE_componentWillUpdate in strict mode is not recommended ' +
'and may indicate bugs in your code. ' +
'See https://fb.me/react-unsafe-component-lifecycles for details.\n\n' +
'* Move data fetching code or side effects to componentDidUpdate.\n' +
'\nPlease update the following components: %s',
sortedNames,
);
}
if (componentWillMountUniqueNames.size > 0) {
const sortedNames = setToSortedString(componentWillMountUniqueNames);
console.warn(
'componentWillMount has been renamed, and is not recommended for use. ' +
'See https://fb.me/react-unsafe-component-lifecycles for details.\n\n' +
'* Move code with side effects to componentDidMount, and set initial state in the constructor.\n' +
'* Rename componentWillMount to UNSAFE_componentWillMount to suppress ' +
'this warning in non-strict mode. In React 17.x, only the UNSAFE_ name will work. ' +
'To rename all deprecated lifecycles to their new names, you can run ' +
'`npx react-codemod rename-unsafe-lifecycles` in your project source folder.\n' +
'\nPlease update the following components: %s',
sortedNames,
);
}
if (componentWillReceivePropsUniqueNames.size > 0) {
const sortedNames = setToSortedString(
componentWillReceivePropsUniqueNames,
);
console.warn(
'componentWillReceiveProps has been renamed, and is not recommended for use. ' +
'See https://fb.me/react-unsafe-component-lifecycles for details.\n\n' +
'* Move data fetching code or side effects to componentDidUpdate.\n' +
"* If you're updating state whenever props change, refactor your " +
'code to use memoization techniques or move it to ' +
'static getDerivedStateFromProps. Learn more at: https://fb.me/react-derived-state\n' +
'* Rename componentWillReceiveProps to UNSAFE_componentWillReceiveProps to suppress ' +
'this warning in non-strict mode. In React 17.x, only the UNSAFE_ name will work. ' +
'To rename all deprecated lifecycles to their new names, you can run ' +
'`npx react-codemod rename-unsafe-lifecycles` in your project source folder.\n' +
'\nPlease update the following components: %s',
sortedNames,
);
}
if (componentWillUpdateUniqueNames.size > 0) {
const sortedNames = setToSortedString(componentWillUpdateUniqueNames);
console.warn(
'componentWillUpdate has been renamed, and is not recommended for use. ' +
'See https://fb.me/react-unsafe-component-lifecycles for details.\n\n' +
'* Move data fetching code or side effects to componentDidUpdate.\n' +
'* Rename componentWillUpdate to UNSAFE_componentWillUpdate to suppress ' +
'this warning in non-strict mode. In React 17.x, only the UNSAFE_ name will work. ' +
'To rename all deprecated lifecycles to their new names, you can run ' +
'`npx react-codemod rename-unsafe-lifecycles` in your project source folder.\n' +
'\nPlease update the following components: %s',
sortedNames,
);
}
};
let pendingLegacyContextWarning: FiberToFiberComponentsMap = new Map();
// Tracks components we have already warned about.
const didWarnAboutLegacyContext = new Set();
ReactStrictModeWarnings.recordLegacyContextWarning = (
fiber: Fiber,
instance: any,
) => {
const strictRoot = findStrictRoot(fiber);
if (strictRoot === null) {
console.error(
'Expected to find a StrictMode component in a strict mode tree. ' +
'This error is likely caused by a bug in React. Please file an issue.',
);
return;
}
// Dedup strategy: Warn once per component.
if (didWarnAboutLegacyContext.has(fiber.type)) {
return;
}
let warningsForRoot = pendingLegacyContextWarning.get(strictRoot);
if (
fiber.type.contextTypes != null ||
fiber.type.childContextTypes != null ||
(instance !== null && typeof instance.getChildContext === 'function')
) {
if (warningsForRoot === undefined) {
warningsForRoot = [];
pendingLegacyContextWarning.set(strictRoot, warningsForRoot);
}
warningsForRoot.push(fiber);
}
};
ReactStrictModeWarnings.flushLegacyContextWarning = () => {
((pendingLegacyContextWarning: any): FiberToFiberComponentsMap).forEach(
(fiberArray: FiberArray, strictRoot) => {
if (fiberArray.length === 0) {
return;
}
const firstFiber = fiberArray[0];
const uniqueNames = new Set();
fiberArray.forEach(fiber => {
uniqueNames.add(getComponentName(fiber.type) || 'Component');
didWarnAboutLegacyContext.add(fiber.type);
});
const sortedNames = setToSortedString(uniqueNames);
const firstComponentStack = getStackByFiberInDevAndProd(firstFiber);
console.error(
'Legacy context API has been detected within a strict-mode tree.' +
'\n\nThe old API will be supported in all 16.x releases, but applications ' +
'using it should migrate to the new version.' +
'\n\nPlease update the following components: %s' +
'\n\nLearn more about this warning here: https://fb.me/react-legacy-context' +
'%s',
sortedNames,
firstComponentStack,
);
},
);
};
ReactStrictModeWarnings.discardPendingWarnings = () => {
pendingComponentWillMountWarnings = [];
pendingUNSAFE_ComponentWillMountWarnings = [];
pendingComponentWillReceivePropsWarnings = [];
pendingUNSAFE_ComponentWillReceivePropsWarnings = [];
pendingComponentWillUpdateWarnings = [];
pendingUNSAFE_ComponentWillUpdateWarnings = [];
pendingLegacyContextWarning = new Map();
};
}
export default ReactStrictModeWarnings;