Skip to content

Commit ce97e99

Browse files
committed
Address more feedback
1 parent 4cf7703 commit ce97e99

File tree

2 files changed

+58
-61
lines changed

2 files changed

+58
-61
lines changed

packages/react-reconciler/src/ReactStrictModeWarnings.js

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ if (__DEV__) {
286286
let pendingLegacyContextWarning: FiberToFiberComponentsMap = new Map();
287287

288288
// Tracks components we have already warned about.
289+
const legacyContextCounts = new Map();
289290
const didWarnAboutLegacyContext = new Set();
290291

291292
ReactStrictModeWarnings.recordLegacyContextWarning = (
@@ -300,17 +301,23 @@ if (__DEV__) {
300301
);
301302
return;
302303
}
304+
const type = fiber.type;
303305

304-
// Dedup strategy: Warn once per component.
305-
if (didWarnAboutLegacyContext.has(fiber.type)) {
306+
// Update legacy context counts
307+
const warningCount = legacyContextCounts.get(type) || 0;
308+
// Increase warning count by 1
309+
legacyContextCounts.set(type, warningCount + 1);
310+
311+
// Dedup strategy: Warn once per component
312+
if (didWarnAboutLegacyContext.has(type)) {
306313
return;
307314
}
308315

309316
let warningsForRoot = pendingLegacyContextWarning.get(strictRoot);
310317

311318
if (
312-
fiber.type.contextTypes != null ||
313-
fiber.type.childContextTypes != null ||
319+
type.contextTypes != null ||
320+
type.childContextTypes != null ||
314321
(instance !== null && typeof instance.getChildContext === 'function')
315322
) {
316323
if (warningsForRoot === undefined) {
@@ -324,49 +331,42 @@ if (__DEV__) {
324331
ReactStrictModeWarnings.flushLegacyContextWarning = () => {
325332
((pendingLegacyContextWarning: any): FiberToFiberComponentsMap).forEach(
326333
(fiberArray: FiberArray, strictRoot) => {
327-
const componentStacks = new Map();
328-
329-
fiberArray.forEach(fiber => {
330-
const componentName = getComponentName(fiber.type) || 'Component';
331-
const componentStack = getStackByFiberInDevAndProd(fiber);
332-
let count = 0;
333-
if (componentStacks.has(componentStack)) {
334-
({count} = (componentStacks.get(componentStack): any));
335-
}
336-
// Increase count by 1
337-
componentStacks.set(componentStack, {
338-
count: count + 1,
339-
name: componentName,
340-
stack: componentStack,
334+
const fibers = fiberArray.length;
335+
let componentNames = '';
336+
337+
fiberArray
338+
.sort((a, b) => {
339+
const aCount = legacyContextCounts.get(a.type);
340+
const bCount = legacyContextCounts.get(b.type);
341+
return bCount - aCount;
342+
})
343+
.forEach((fiber, i) => {
344+
const type = fiber.type;
345+
if (didWarnAboutLegacyContext.has(type)) {
346+
return;
347+
}
348+
didWarnAboutLegacyContext.add(type);
349+
// Build up the string of comma separated component names
350+
componentNames +=
351+
(getComponentName(fiber.type) || 'Component') +
352+
(i === fibers - 1 ? '' : ', ');
341353
});
342-
didWarnAboutLegacyContext.add(fiber.type);
343-
});
344-
345-
// Get the stacks from our componentStacks Map
346-
const stacks = Array.from(componentStacks.values());
347-
348-
// Sort the stacks by their counts
349-
stacks.sort((a, b) => b.count - a.count);
350-
351-
const mostFrequentStack = stacks[0];
352-
353-
if (mostFrequentStack) {
354-
// We map to a Set to remove duplicate component names
355-
const componentNames = Array.from(
356-
new Set(stacks.map(stack => stack.name)),
357-
).join(', ');
358-
359-
console.error(
360-
'Legacy context API has been detected within a strict-mode tree.' +
361-
'\n\nThe old API will be supported in all 16.x releases, but applications ' +
362-
'using it should migrate to the new version.' +
363-
'\n\nPlease update the following components: %s' +
364-
'\n\nLearn more about this warning here: https://fb.me/react-legacy-context' +
365-
'%s',
366-
componentNames,
367-
mostFrequentStack.stack,
368-
);
369-
}
354+
const mostFrequentFiber = fiberArray[0];
355+
// We map to a Set to remove duplicate component name
356+
const stack = mostFrequentFiber
357+
? getStackByFiberInDevAndProd(mostFrequentFiber)
358+
: '';
359+
360+
console.error(
361+
'Legacy context API has been detected within a strict-mode tree.' +
362+
'\n\nThe old API will be supported in all 16.x releases, but applications ' +
363+
'using it should migrate to the new version.' +
364+
'\n\nPlease update the following components: %s' +
365+
'\n\nLearn more about this warning here: https://fb.me/react-legacy-context' +
366+
'%s',
367+
componentNames,
368+
stack,
369+
);
370370
},
371371
);
372372
};

packages/react-reconciler/src/__tests__/ReactIncremental-test.internal.js

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2036,20 +2036,17 @@ describe('ReactIncremental', () => {
20362036
};
20372037

20382038
ReactNoop.render(<Recurse />);
2039-
expect(() => expect(Scheduler).toFlushWithoutYielding()).toErrorDev(
2040-
[
2041-
'Warning: The <Recurse /> component appears to be a function component that returns a class instance. ' +
2042-
'Change Recurse to a class that extends React.Component instead. ' +
2043-
"If you can't use a class try assigning the prototype on the function as a workaround. " +
2044-
'`Recurse.prototype = React.Component.prototype`. ' +
2045-
"Don't use an arrow function since it cannot be called with `new` by React.",
2046-
'Legacy context API has been detected within a strict-mode tree.\n\n' +
2047-
'The old API will be supported in all 16.x releases, but applications ' +
2048-
'using it should migrate to the new version.\n\n' +
2049-
'Please update the following components: Recurse',
2050-
],
2051-
{withoutStack: 0},
2052-
);
2039+
expect(() => expect(Scheduler).toFlushWithoutYielding()).toErrorDev([
2040+
'Warning: The <Recurse /> component appears to be a function component that returns a class instance. ' +
2041+
'Change Recurse to a class that extends React.Component instead. ' +
2042+
"If you can't use a class try assigning the prototype on the function as a workaround. " +
2043+
'`Recurse.prototype = React.Component.prototype`. ' +
2044+
"Don't use an arrow function since it cannot be called with `new` by React.",
2045+
'Legacy context API has been detected within a strict-mode tree.\n\n' +
2046+
'The old API will be supported in all 16.x releases, but applications ' +
2047+
'using it should migrate to the new version.\n\n' +
2048+
'Please update the following components: Recurse',
2049+
]);
20532050
expect(ops).toEqual([
20542051
'Recurse {}',
20552052
'Recurse {"n":2}',
@@ -2114,7 +2111,7 @@ describe('ReactIncremental', () => {
21142111
'Legacy context API has been detected within a strict-mode tree.\n\n' +
21152112
'The old API will be supported in all 16.x releases, but applications ' +
21162113
'using it should migrate to the new version.\n\n' +
2117-
'Please update the following components: Intl, ShowLocale',
2114+
'Please update the following components: ShowLocale, Intl',
21182115
);
21192116
});
21202117

0 commit comments

Comments
 (0)