From e9ae68f55b55c5a0c7b74c6af00e2e5c5b3d99a6 Mon Sep 17 00:00:00 2001 From: Alex Krolick Date: Thu, 22 Mar 2018 20:58:22 -0700 Subject: [PATCH 01/12] remove confusing {} --- content/docs/context.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/docs/context.md b/content/docs/context.md index 2b9ccfdd..55997c8d 100644 --- a/content/docs/context.md +++ b/content/docs/context.md @@ -55,7 +55,7 @@ Accepts a `value` prop to be passed to Consumers that are descendants of this Pr ```js - { value => { /* render something based on the context value */ } } + {value => /* render something based on the context value */} ``` From 53d604fd3a7209b2a9c1293ce094b867b7d8983d Mon Sep 17 00:00:00 2001 From: Alex Krolick Date: Thu, 22 Mar 2018 20:58:50 -0700 Subject: [PATCH 02/12] ~deprecated~ legacy --- content/docs/context.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/docs/context.md b/content/docs/context.md index 55997c8d..2f0b3c60 100644 --- a/content/docs/context.md +++ b/content/docs/context.md @@ -90,6 +90,6 @@ A more complex example with dynamic values for the theme: ## Legacy API -> The legacy context API was deprecated in React 16.3 and will be removed in version 17. +> The old context API was marked as legacy in React 16.3 and will be removed in version 17. > > React previously shipped with an experimental context API. The old API will be supported in all 16.x releases, but applications using it should migrate to the new version. Read the [legacy context docs here](/docs/legacy-context.html). From fec6d6c141def553e71aa189da30aa687db31035 Mon Sep 17 00:00:00 2001 From: Alex Krolick Date: Thu, 22 Mar 2018 21:01:59 -0700 Subject: [PATCH 03/12] Add mutliple contexts example --- content/docs/context.md | 3 +++ examples/context/multiple-contexts.js | 36 +++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 examples/context/multiple-contexts.js diff --git a/content/docs/context.md b/content/docs/context.md index 2f0b3c60..7c158a4e 100644 --- a/content/docs/context.md +++ b/content/docs/context.md @@ -88,6 +88,9 @@ A more complex example with dynamic values for the theme: **app.js** `embed:context/theme-detailed-app.js` +## Consuming Multiple Contexts + +`embed:context/multiple-contexts.js` ## Legacy API > The old context API was marked as legacy in React 16.3 and will be removed in version 17. diff --git a/examples/context/multiple-contexts.js b/examples/context/multiple-contexts.js new file mode 100644 index 00000000..494e8cd4 --- /dev/null +++ b/examples/context/multiple-contexts.js @@ -0,0 +1,36 @@ +// Create a theme context, defaulting to light theme +// highlight-next-line +const ThemeContext = React.createContext('light'); + +// Signed-in user context +const UserContext = React.createContext(); + +class App extends React.Component { + static propTypes = { + theme: PropTypes.string, + signedInUser: PropTypes.shape({ + id: number, + name: string, + avatar: string, + }), + }; + + render() { + return ( + + + + {theme => ( + + {user => ( + + )} + + )} + + + + ); + } +} From 7b5764fed5c35cf7b3fcc5df480abe56298852bd Mon Sep 17 00:00:00 2001 From: Alex Krolick Date: Thu, 22 Mar 2018 21:15:40 -0700 Subject: [PATCH 04/12] Add ref forwarding, lifecycle examples --- content/docs/context.md | 12 ++++++++++++ examples/context/forwarding-refs.js | 11 +++++++++++ examples/context/lifecycles.js | 20 ++++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 examples/context/forwarding-refs.js create mode 100644 examples/context/lifecycles.js diff --git a/content/docs/context.md b/content/docs/context.md index 7c158a4e..4441118d 100644 --- a/content/docs/context.md +++ b/content/docs/context.md @@ -16,6 +16,9 @@ In a typical React application, data is passed top-down (parent to child) via pr - [Examples](#examples) - [Static Context](#static-context) - [Dynamic Context](#dynamic-context) + - [Consuming Multiple Contexts](#consuming-multiple-contexts) + - [Accessing Context in Lifecycle Methods](#accessing-context-in-lifecycle-methods) + - [Forwarding Refs to Context Consumers](#forwarding-refs-to-context-consumers) - [Legacy API](#legacy-api) @@ -91,6 +94,15 @@ A more complex example with dynamic values for the theme: ## Consuming Multiple Contexts `embed:context/multiple-contexts.js` + +## Accessing Context in Lifecycle Methods + +`embed:context/lifecycles.js` + +## Forwarding Refs to Context Consumers + +`embed:context/forwarding-refs.js` + ## Legacy API > The old context API was marked as legacy in React 16.3 and will be removed in version 17. diff --git a/examples/context/forwarding-refs.js b/examples/context/forwarding-refs.js new file mode 100644 index 00000000..e23ae77f --- /dev/null +++ b/examples/context/forwarding-refs.js @@ -0,0 +1,11 @@ +const Button = ({theme, children}) => ( + +); + +export default React.forwardRef((props, ref) => ( + + {theme => + ); + } +} + +export default props => ( + + {theme => - ); - } -} +const ThemedButton = props => { + //highlight-range{1} + return - - ); - } -} +// An intermediate component +const Toolbar = props => { + // highlight-range{1-2,5} + // The Toolbar component must take an extra theme prop + // and pass it to the ThemedButton + return ( +
+ +
+ ); +}; -class MessageList extends React.Component { +class App extends React.Component { render() { - const color = 'purple'; - const children = this.props.messages.map(message => ( - - )); - return
{children}
; + // highlight-range{1} + return ; } } diff --git a/examples/context/motivation-solution.js b/examples/context/motivation-solution.js index 76bf72cc..d1922f6d 100644 --- a/examples/context/motivation-solution.js +++ b/examples/context/motivation-solution.js @@ -1,43 +1,31 @@ -// highlight-range{1} -const ColorContext = React.createContext(); +// Create a theme context, defaulting to light theme +// highlight-next-line +const ThemeContext = React.createContext('light'); -class Button extends React.Component { - render() { - // highlight-range{2-8} - return ( - - {color => ( - - )} - - ); - } -} +// highlight-range{1,3-5} +// The ThemedButton receives the theme from context +const ThemedButton = props => ( + + {theme => - - ); - } -} +// An intermediate component +const Toolbar = props => { + return ( +
+ +
+ ); +}; -class MessageList extends React.Component { +class App extends React.Component { render() { - const color = 'purple'; - const children = this.props.messages.map(message => ( - - )); - // highlight-range{2-4} + // highlight-range{2,4} return ( - - {children} - + + + ); } } diff --git a/examples/context/theme-detailed-app.js b/examples/context/theme-detailed-app.js index 1b57e15d..07b3de48 100644 --- a/examples/context/theme-detailed-app.js +++ b/examples/context/theme-detailed-app.js @@ -1,6 +1,15 @@ import {ThemeContext, themes} from './theme-context'; import ThemedButton from './button'; +// An intermediate component that uses the ThemedButton +const Toolbar = props => { + return ( + + Change Theme + + ); +}; + class App extends React.Component { state = { theme: themes.light, @@ -16,13 +25,20 @@ class App extends React.Component { }; render() { - //highlight-range{2,6} + //highlight-range{1-3} + // The ThemedButton button inside the ThemeProvider + // uses the theme from state while the one outside uses + // the default dark theme + //highlight-range{3-5,7} return ( - - - Change Theme - - +
+ + + +
+ +
+
); } } diff --git a/examples/context/theme-example.js b/examples/context/theme-example.js deleted file mode 100644 index d96e4765..00000000 --- a/examples/context/theme-example.js +++ /dev/null @@ -1,55 +0,0 @@ -// Create a theme context, defaulting to light theme -// highlight-next-line -const ThemeContext = React.createContext('light'); - -class ThemeProvider extends React.Component { - render() { - // highlight-range{2-4} - return ( - - {this.props.children} - - ); - } -} - -class ThemedButton extends React.Component { - render() { - //highlight-range{2-4} - return ( - - {theme => ); +// highlight-range{1,3} export default React.forwardRef((props, ref) => ( {theme =>