From f7cb9d2b2291b2b4725786105ddd89316711161f Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Mon, 15 Oct 2018 18:31:14 -0400 Subject: [PATCH] Warn about useContext(Consumer|Provider) --- .../ReactNewContext-test.internal.js | 235 ++++++++++++++---- packages/react/src/ReactHooks.js | 22 ++ 2 files changed, 203 insertions(+), 54 deletions(-) diff --git a/packages/react-reconciler/src/__tests__/ReactNewContext-test.internal.js b/packages/react-reconciler/src/__tests__/ReactNewContext-test.internal.js index 4ebcd1f84d..4cc8b4b15f 100644 --- a/packages/react-reconciler/src/__tests__/ReactNewContext-test.internal.js +++ b/packages/react-reconciler/src/__tests__/ReactNewContext-test.internal.js @@ -65,16 +65,13 @@ describe('ReactNewContext', () => { return render(contextValue); }), ); - sharedContextTests( - 'useContext inside memoized function component', - Context => - React.memo(function Consumer(props) { - const observedBits = props.unstable_observedBits; - const contextValue = useContext(Context, observedBits); - const render = props.children; - return render(contextValue); - }), - true, + sharedContextTests('useContext inside memoized function component', Context => + React.memo(function Consumer(props) { + const observedBits = props.unstable_observedBits; + const contextValue = useContext(Context, observedBits); + const render = props.children; + return render(contextValue); + }), ); sharedContextTests( 'readContext(Context) inside class component', @@ -88,8 +85,20 @@ describe('ReactNewContext', () => { } }, ); + sharedContextTests( + 'readContext(Context) inside pure class component', + Context => + class Consumer extends React.PureComponent { + render() { + const observedBits = this.props.unstable_observedBits; + const contextValue = readContext(Context, observedBits); + const render = this.props.children; + return render(contextValue); + } + }, + ); - function sharedContextTests(label, getConsumer, isPure) { + function sharedContextTests(label, getConsumer) { describe(`reading context with ${label}`, () => { it('simple mount and update', () => { const Context = React.createContext(1); @@ -874,49 +883,6 @@ describe('ReactNewContext', () => { expect(ReactNoop.getChildren()).toEqual([span(2), span(2)]); }); - if (!isPure) { - // Context consumer bails out on propagating "deep" updates when `value` hasn't changed. - // However, it doesn't bail out from rendering if the component above it re-rendered anyway. - // If we bailed out on referential equality, it would be confusing that you - // can call this.setState(), but an autobound render callback "blocked" the update. - // https://github.com/facebook/react/pull/12470#issuecomment-376917711 - it('consumer does not bail out if there were no bailouts above it', () => { - const Context = React.createContext(0); - const Consumer = getConsumer(Context); - - class App extends React.Component { - state = { - text: 'hello', - }; - - renderConsumer = context => { - ReactNoop.yield('App#renderConsumer'); - return ; - }; - - render() { - ReactNoop.yield('App'); - return ( - - {this.renderConsumer} - - ); - } - } - - // Initial mount - let inst; - ReactNoop.render( (inst = ref)} />); - expect(ReactNoop.flush()).toEqual(['App', 'App#renderConsumer']); - expect(ReactNoop.getChildren()).toEqual([span('hello')]); - - // Update - inst.setState({text: 'goodbye'}); - expect(ReactNoop.flush()).toEqual(['App', 'App#renderConsumer']); - expect(ReactNoop.getChildren()).toEqual([span('goodbye')]); - }); - } - // This is a regression case for https://github.com/facebook/react/issues/12389. it('does not run into an infinite loop', () => { const Context = React.createContext(null); @@ -1260,6 +1226,96 @@ describe('ReactNewContext', () => { expect(ReactNoop.flush()).toEqual(['Foo: 2, Bar: 2']); expect(ReactNoop.getChildren()).toEqual([span('Foo: 2, Bar: 2')]); }); + + // Context consumer bails out on propagating "deep" updates when `value` hasn't changed. + // However, it doesn't bail out from rendering if the component above it re-rendered anyway. + // If we bailed out on referential equality, it would be confusing that you + // can call this.setState(), but an autobound render callback "blocked" the update. + // https://github.com/facebook/react/pull/12470#issuecomment-376917711 + it('consumer does not bail out if there were no bailouts above it', () => { + const Context = React.createContext(0); + const Consumer = Context.Consumer; + + class App extends React.Component { + state = { + text: 'hello', + }; + + renderConsumer = context => { + ReactNoop.yield('App#renderConsumer'); + return ; + }; + + render() { + ReactNoop.yield('App'); + return ( + + {this.renderConsumer} + + ); + } + } + + // Initial mount + let inst; + ReactNoop.render( (inst = ref)} />); + expect(ReactNoop.flush()).toEqual(['App', 'App#renderConsumer']); + expect(ReactNoop.getChildren()).toEqual([span('hello')]); + + // Update + inst.setState({text: 'goodbye'}); + expect(ReactNoop.flush()).toEqual(['App', 'App#renderConsumer']); + expect(ReactNoop.getChildren()).toEqual([span('goodbye')]); + }); + }); + + describe('readContext', () => { + // Context consumer bails out on propagating "deep" updates when `value` hasn't changed. + // However, it doesn't bail out from rendering if the component above it re-rendered anyway. + // If we bailed out on referential equality, it would be confusing that you + // can call this.setState(), but an autobound render callback "blocked" the update. + // https://github.com/facebook/react/pull/12470#issuecomment-376917711 + it('does not bail out if there were no bailouts above it', () => { + const Context = React.createContext(0); + + class Consumer extends React.Component { + render() { + const contextValue = readContext(Context); + return this.props.children(contextValue); + } + } + + class App extends React.Component { + state = { + text: 'hello', + }; + + renderConsumer = context => { + ReactNoop.yield('App#renderConsumer'); + return ; + }; + + render() { + ReactNoop.yield('App'); + return ( + + {this.renderConsumer} + + ); + } + } + + // Initial mount + let inst; + ReactNoop.render( (inst = ref)} />); + expect(ReactNoop.flush()).toEqual(['App', 'App#renderConsumer']); + expect(ReactNoop.getChildren()).toEqual([span('hello')]); + + // Update + inst.setState({text: 'goodbye'}); + expect(ReactNoop.flush()).toEqual(['App', 'App#renderConsumer']); + expect(ReactNoop.getChildren()).toEqual([span('goodbye')]); + }); }); describe('useContext', () => { @@ -1366,6 +1422,77 @@ describe('ReactNewContext', () => { 'Hooks can only be called inside the body of a function component.', ); }); + + it('warns when passed a consumer', () => { + const Context = React.createContext(0); + function Foo() { + return useContext(Context.Consumer); + } + ReactNoop.render(); + expect(ReactNoop.flush).toWarnDev( + 'Calling useContext(Context.Consumer) is not supported, may cause bugs, ' + + 'and will be removed in a future major release. ' + + 'Did you mean to call useContext(Context) instead?', + ); + }); + + it('warns when passed a provider', () => { + const Context = React.createContext(0); + function Foo() { + useContext(Context.Provider); + return null; + } + ReactNoop.render(); + expect(ReactNoop.flush).toWarnDev( + 'Calling useContext(Context.Provider) is not supported. ' + + 'Did you mean to call useContext(Context) instead?', + ); + }); + + // Context consumer bails out on propagating "deep" updates when `value` hasn't changed. + // However, it doesn't bail out from rendering if the component above it re-rendered anyway. + // If we bailed out on referential equality, it would be confusing that you + // can call this.setState(), but an autobound render callback "blocked" the update. + // https://github.com/facebook/react/pull/12470#issuecomment-376917711 + it('does not bail out if there were no bailouts above it', () => { + const Context = React.createContext(0); + + function Consumer({children}) { + const contextValue = useContext(Context); + return children(contextValue); + } + + class App extends React.Component { + state = { + text: 'hello', + }; + + renderConsumer = context => { + ReactNoop.yield('App#renderConsumer'); + return ; + }; + + render() { + ReactNoop.yield('App'); + return ( + + {this.renderConsumer} + + ); + } + } + + // Initial mount + let inst; + ReactNoop.render( (inst = ref)} />); + expect(ReactNoop.flush()).toEqual(['App', 'App#renderConsumer']); + expect(ReactNoop.getChildren()).toEqual([span('hello')]); + + // Update + inst.setState({text: 'goodbye'}); + expect(ReactNoop.flush()).toEqual(['App', 'App#renderConsumer']); + expect(ReactNoop.getChildren()).toEqual([span('goodbye')]); + }); }); it('unwinds after errors in complete phase', () => { diff --git a/packages/react/src/ReactHooks.js b/packages/react/src/ReactHooks.js index e2ccc3a2a8..1cc8b79ac5 100644 --- a/packages/react/src/ReactHooks.js +++ b/packages/react/src/ReactHooks.js @@ -9,6 +9,7 @@ import type {ReactContext} from 'shared/ReactTypes'; import invariant from 'shared/invariant'; +import warning from 'shared/warning'; import ReactCurrentOwner from './ReactCurrentOwner'; @@ -26,6 +27,27 @@ export function useContext( observedBits: number | boolean | void, ) { const dispatcher = resolveDispatcher(); + if (__DEV__) { + // TODO: add a more generic warning for invalid values. + if ((Context: any)._context !== undefined) { + const realContext = (Context: any)._context; + // Don't deduplicate because this legitimately causes bugs + // and nobody should be using this in existing code. + if (realContext.Consumer === Context) { + warning( + false, + 'Calling useContext(Context.Consumer) is not supported, may cause bugs, and will be ' + + 'removed in a future major release. Did you mean to call useContext(Context) instead?', + ); + } else if (realContext.Provider === Context) { + warning( + false, + 'Calling useContext(Context.Provider) is not supported. ' + + 'Did you mean to call useContext(Context) instead?', + ); + } + } + } return dispatcher.useContext(Context, observedBits); }