@@ -21,13 +21,17 @@ type LIFECYCLE =
2121 | 'UNSAFE_componentWillUpdate' ;
2222type LifecycleToComponentsMap = { [ lifecycle : LIFECYCLE ] : Array < Fiber > } ;
2323type FiberToLifecycleMap = Map < Fiber , LifecycleToComponentsMap > ;
24+ type FiberArray = Array < Fiber > ;
25+ type FiberToFiberComponentsMap = Map < Fiber , FiberArray > ;
2426
2527const ReactStrictModeWarnings = {
2628 discardPendingWarnings ( ) : void { } ,
2729 flushPendingDeprecationWarnings ( ) : void { } ,
2830 flushPendingUnsafeLifecycleWarnings ( ) : void { } ,
2931 recordDeprecationWarnings ( fiber : Fiber , instance : any ) : void { } ,
3032 recordUnsafeLifecycleWarnings ( fiber : Fiber , instance : any ) : void { } ,
33+ recordLegacyContextWarning ( fiber : Fiber , instance : any ) : void { } ,
34+ flushLegacyContextWarning ( ) : void { } ,
3135} ;
3236
3337if ( __DEV__ ) {
@@ -41,10 +45,12 @@ if (__DEV__) {
4145 let pendingComponentWillReceivePropsWarnings : Array < Fiber > = [ ] ;
4246 let pendingComponentWillUpdateWarnings : Array < Fiber > = [ ] ;
4347 let pendingUnsafeLifecycleWarnings : FiberToLifecycleMap = new Map ( ) ;
48+ let pendingLegacyContextWarning : FiberToFiberComponentsMap = new Map ( ) ;
4449
4550 // Tracks components we have already warned about.
4651 const didWarnAboutDeprecatedLifecycles = new Set ( ) ;
4752 const didWarnAboutUnsafeLifecycles = new Set ( ) ;
53+ const didWarnAboutLegacyContext = new Set ( ) ;
4854
4955 const setToSortedString = set => {
5056 const array = [ ] ;
@@ -59,6 +65,7 @@ if (__DEV__) {
5965 pendingComponentWillReceivePropsWarnings = [ ] ;
6066 pendingComponentWillUpdateWarnings = [ ] ;
6167 pendingUnsafeLifecycleWarnings = new Map ( ) ;
68+ pendingLegacyContextWarning = new Map ( ) ;
6269 } ;
6370
6471 ReactStrictModeWarnings . flushPendingUnsafeLifecycleWarnings = ( ) => {
@@ -289,6 +296,67 @@ if (__DEV__) {
289296 } ) ;
290297 }
291298 } ;
299+
300+ ReactStrictModeWarnings . recordLegacyContextWarning = (
301+ fiber : Fiber ,
302+ instance : any ,
303+ ) => {
304+ const strictRoot = findStrictRoot ( fiber ) ;
305+ if ( strictRoot === null ) {
306+ warning (
307+ false ,
308+ 'Expected to find a StrictMode component in a strict mode tree. ' +
309+ 'This error is likely caused by a bug in React. Please file an issue.' ,
310+ ) ;
311+ return ;
312+ }
313+
314+ // Dedup strategy: Warn once per component.
315+ if ( didWarnAboutLegacyContext . has ( fiber . type ) ) {
316+ return ;
317+ }
318+
319+ let warningsForRoot = pendingLegacyContextWarning . get ( strictRoot ) ;
320+
321+ if (
322+ typeof instance . getChildContext === 'function' ||
323+ fiber . type . contextTypes != null ||
324+ fiber . type . childContextTypes != null
325+ ) {
326+ if ( warningsForRoot === undefined ) {
327+ warningsForRoot = [ ] ;
328+ pendingLegacyContextWarning . set ( strictRoot , warningsForRoot ) ;
329+ }
330+ warningsForRoot . push ( fiber ) ;
331+ }
332+ } ;
333+
334+ ReactStrictModeWarnings . flushLegacyContextWarning = ( ) => {
335+ ( ( pendingLegacyContextWarning : any ) : FiberToFiberComponentsMap ) . forEach (
336+ ( fiberArray : FiberArray , strictRoot ) => {
337+ const uniqueNames = new Set ( ) ;
338+ fiberArray . forEach ( fiber => {
339+ uniqueNames . add ( getComponentName ( fiber ) || 'Component' ) ;
340+ didWarnAboutLegacyContext . add ( fiber . type ) ;
341+ } ) ;
342+
343+ const sortedNames = setToSortedString ( uniqueNames ) ;
344+ const strictRootComponentStack = getStackAddendumByWorkInProgressFiber (
345+ strictRoot ,
346+ ) ;
347+
348+ warning (
349+ false ,
350+ 'Legacy context API has been detected within a strict-mode tree: %s' +
351+ '\n\nPlease update the following components: %s' +
352+ '\n\nLearn more about this warning here:' +
353+ '\nhttps://fb.me/react-strict-mode-warnings' ,
354+ strictRootComponentStack ,
355+ sortedNames ,
356+ ) ;
357+ } ,
358+ ) ;
359+ } ;
292360}
293361
294362export default ReactStrictModeWarnings ;
0 commit comments