diff --git a/content/docs/context.md b/content/docs/context.md index 706a6370..fd3e814b 100644 --- a/content/docs/context.md +++ b/content/docs/context.md @@ -6,6 +6,9 @@ permalink: docs/context.html Context provides a way to pass data through the component tree without having to pass props down manually at every level. +Typically, data in a React application is passed top-down (parent to child) via props. But sometimes it's useful to pass values through multiple levels without adding props to every intermediate component. Examples include a language preference, or a UI theme. Many components may rely on those but you don't want to have to pass a locale prop and a theme prop through every level of the tree. + +- [Motivation](#motivation) - [API](#api) - [React.createContext](#reactcreatecontext) - [Provider](#provider) @@ -13,21 +16,26 @@ Context provides a way to pass data through the component tree without having to - [Examples](#examples) - [Static Context](#static-context) - [Dynamic Context](#dynamic-context) -- [Motivation](#motivation) - [Legacy API](#legacy-api) + +## Motivation + +Context is designed to relieve the pain of passing props down through a deeply nested component tree. For example, in the code below we manually thread through a color prop in order to style the Button and Message components. Using context, we can avoid passing props through intermediate elements. + +`embed:context/motivation.js` + ## API ### `React.createContext` ```js -const {Provider, Consumer} = React.createContext([default]); +const {Provider, Consumer} = React.createContext(defaultValue); ``` Creates a `{ Provider, Consumer }` pair. -Takes one argument, the default context that Consumers will receive when they don't have a matching Provider. - +Optionally accepts a default value to be passed to Consumers without a Provider ancestor. ### `Provider` @@ -37,7 +45,7 @@ Takes one argument, the default context that Consumers will receive when they do A React component that allows Consumers to subscribe to context changes. -Takes one prop, `value`, which will be passed to the [render prop](/docs/render-props.html) of child Consumers for the matching context anywhere in the component tree. One Provider can be connected to many Consumers. +Accepts a `value` prop to be passed to Consumers that are descendants of this Provider. One Provider can be connected to many Consumers. Providers can be nested to override values deeper within the tree. ### `Consumer` @@ -47,12 +55,12 @@ Takes one prop, `value`, which will be passed to the [render prop](/docs/render- ``` -A React component that subscribes to context changes. +A React component that subscribes to context changes. -Takes a function as the `children` prop that receives the `value` prop of the matching Provider. This function will be called whenever the Provider's value is updated. +Requires a [function as a child](/docs/render-props.html#using-props-other-than-render). This function receives the current context value and returns a React node, and will be called whenever the Provider's value is updated. > Note: -> +> > For more information about this pattern, see [render props](/docs/render-props.html). ## Examples @@ -76,49 +84,8 @@ A more complex example with dynamic values for the theme: **app.js** `embed:context/theme-detailed-app.js` -## Motivation - -Context is designed to relieve the pain of passing props down through a deeply nested component tree. For example, in the code below we manually thread through a color prop in order to style the Button and Message components. Using context, we can avoid passing props through intermediate elements. - -```js -class Button extends React.Component { - render() { - return ( - - ); - } -} - -class Message extends React.Component { - render() { - return ( -
- {/* - The Message component must take `color` as as prop to pass it to the - Button. Using context, the Button could connect to the color context - on its own. - */} - {this.props.text} -
- ); - } -} - -class MessageList extends React.Component { - render() { - const color = "purple"; - const children = this.props.messages.map((message) => - - ); - return
{children}
; - } -} -``` - ## Legacy API -> The legacy context API was deprecated in React 16.3 +> The legacy context API was deprecated 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). diff --git a/content/docs/legacy-context.md b/content/docs/legacy-context.md index d9e899f0..8da900b6 100644 --- a/content/docs/legacy-context.md +++ b/content/docs/legacy-context.md @@ -4,9 +4,11 @@ title: Legacy Context permalink: docs/legacy-context.html --- -> This API is deprecated as of React 16.3. -> -> The API will be supported in all 16.x releases, but applications using it should migrate to the [new API](/docs/context.html). The experimental API lacked a safe mechanism to update context. Version 16.3 introduced a new context API that is more efficient and supports both static type checking and deep updates. +> Deprecation Warning +> +> The legacy API has been deprecated and will be removed in version 17. +> Use the [new context API](/docs/context.html) introduced with version 16.3. +> The legacy API will continue working for all 16.x releases. ## How To Use Context diff --git a/examples/context/motivation.js b/examples/context/motivation.js new file mode 100644 index 00000000..1e6ed885 --- /dev/null +++ b/examples/context/motivation.js @@ -0,0 +1,33 @@ +class Button extends React.Component { + render() { + return ( + + ); + } +} + +class Message extends React.Component { + render() { + // highlight-range{1-3} + // The Message component must take `color` as as prop to pass it + // to the Button. Using context, the Button could connect to the + // color context on its own. + return ( +
+ {this.props.text} +
+ ); + } +} + +class MessageList extends React.Component { + render() { + const color = "purple"; + const children = this.props.messages.map((message) => + + ); + return
{children}
; + } +} diff --git a/examples/context/theme-detailed-app.js b/examples/context/theme-detailed-app.js index 4cbd4669..e1ca8345 100644 --- a/examples/context/theme-detailed-app.js +++ b/examples/context/theme-detailed-app.js @@ -1,34 +1,27 @@ -import ThemeContext from './theme-context'; +import ThemeContext, {themes} from './theme-context'; import ThemedButton from './button'; class App extends React.Component { state = { - theme: { - highlight: 'blue', - accent: 'purple', - }, + theme: themes.light, }; - changeHighlightColor = () => { - const colors = ['red', 'blue', 'green']; - const randomColor = - colors[Math.floor(Math.random() * 3)]; - this.setState({ - theme: { - ...this.state.theme, - highlight: randomColor, - }, - }); + toggleTheme = () => { + this.setState(state => ({ + theme: + state.theme === themes.dark + ? themes.light + : themes.dark, + })); }; render() { + //highlight-range{2,6} return ( -
- - Change Theme - -
+ + Change Theme +
); } diff --git a/examples/context/theme-detailed-theme-context.js b/examples/context/theme-detailed-theme-context.js index 75af25f8..d8051fab 100644 --- a/examples/context/theme-detailed-theme-context.js +++ b/examples/context/theme-detailed-theme-context.js @@ -1,5 +1,15 @@ -const defaultTheme = {highlight: 'blue', accent: 'purple'}; +export const themes = { + light: { + foreground: '#ffffff', + background: '#222222', + }, + dark: { + foreground: '#000000', + background: '#eeeeee', + }, +}; -const ThemeContext = React.createContext(defaultTheme); +// highlight-next-line +const ThemeContext = React.createContext(themes.dark); export default ThemeContext; diff --git a/examples/context/theme-detailed-themed-button.js b/examples/context/theme-detailed-themed-button.js index 6ec33b5b..10c1d922 100644 --- a/examples/context/theme-detailed-themed-button.js +++ b/examples/context/theme-detailed-themed-button.js @@ -1,13 +1,14 @@ import ThemeContext from './theme-context'; class ThemedButton extends React.Component { + // highlight-range{3-10} render() { return ( {theme => (