diff --git a/packages/react-reconciler/src/ReactFiberBeginWork.js b/packages/react-reconciler/src/ReactFiberBeginWork.js index 035193cfcd..8a26c84b39 100644 --- a/packages/react-reconciler/src/ReactFiberBeginWork.js +++ b/packages/react-reconciler/src/ReactFiberBeginWork.js @@ -1118,13 +1118,18 @@ function updateContextConsumer( // in DEV mode if this property exists or not and warn if it does not. if (__DEV__) { if ((context: any)._context === undefined) { - if (!hasWarnedAboutUsingContextAsConsumer) { - hasWarnedAboutUsingContextAsConsumer = true; - warning( - false, - 'Rendering directly is not supported and will be removed in ' + - 'a future major release. Did you mean to render instead?', - ); + // This may be because it's a Context (rather than a Consumer). + // Or it may be because it's older React where they're the same thing. + // We only want to warn if we're sure it's a new React. + if (context !== context.Consumer) { + if (!hasWarnedAboutUsingContextAsConsumer) { + hasWarnedAboutUsingContextAsConsumer = true; + warning( + false, + 'Rendering directly is not supported and will be removed in ' + + 'a future major release. Did you mean to render instead?', + ); + } } } else { context = (context: any)._context; diff --git a/packages/react-reconciler/src/__tests__/ReactNewContext-test.internal.js b/packages/react-reconciler/src/__tests__/ReactNewContext-test.internal.js index 03b52dccdc..52ee23f5ff 100644 --- a/packages/react-reconciler/src/__tests__/ReactNewContext-test.internal.js +++ b/packages/react-reconciler/src/__tests__/ReactNewContext-test.internal.js @@ -1584,6 +1584,28 @@ Context fuzz tester error! Copy and paste the following line into the test suite ); }); + // False positive regression test. + it('should not warn when using Consumer from React < 16.6 with newer renderer', () => { + const BarContext = React.createContext({value: 'bar-initial'}); + // React 16.5 and earlier didn't have a separate object. + BarContext.Consumer = BarContext; + + function Component() { + return ( + + + + {({value}) =>
} + + + + ); + } + + ReactNoop.render(); + ReactNoop.flush(); + }); + it('should warn with an error message when using nested context consumers in DEV', () => { const BarContext = React.createContext({value: 'bar-initial'}); const BarConsumer = BarContext;