@@ -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,41 @@ 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+ const stack = mostFrequentFiber
356+ ? getStackByFiberInDevAndProd ( mostFrequentFiber )
357+ : '' ;
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+ stack ,
368+ ) ;
370369 } ,
371370 ) ;
372371 } ;
0 commit comments