Skip to content

Commit 566406e

Browse files
committed
Add docs for new Context API (R16.3)
1 parent 11834be commit 566406e

File tree

5 files changed

+172
-20
lines changed

5 files changed

+172
-20
lines changed

content/docs/context.md

Lines changed: 70 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,71 @@ title: Context
44
permalink: docs/context.html
55
---
66

7-
> Note:
8-
>
9-
> `React.PropTypes` has moved into a different package since React v15.5. Please use [the `prop-types` library instead](https://www.npmjs.com/package/prop-types) to define `contextTypes`.
10-
>
11-
>We provide [a codemod script](/blog/2017/04/07/react-v15.5.0.html#migrating-from-react.proptypes) to automate the conversion.
7+
Context provides a way to pass data through the component tree without having to pass props down manually at every level.
8+
9+
## Using Context
10+
11+
Here is an example illustrating how you might inject a "theme" using context:
12+
13+
`embed:context/theme-example.js`
14+
15+
### API
16+
17+
#### `React.createContext`
18+
19+
```js
20+
const {Provider, Consumer} = React.createContext([default]);
21+
```
22+
23+
Creates a `{ Provider, Consumer }` pair.
24+
25+
Takes one argument, the default context that Consumers will receive when they don't have a matching Provider.
26+
27+
28+
#### `Provider`
29+
30+
```js
31+
<Provider value={/* some value */}>
32+
```
1233

13-
With React, it's easy to track the flow of data through your React components. When you look at a component, you can see which props are being passed, which makes your apps easy to reason about.
34+
A React component that allows Consumers to subscribe to context changes.
1435

15-
In some cases, you want to pass data through the component tree without having to pass the props down manually at every level.
16-
You can do this directly in React with the powerful "context" API.
36+
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.
37+
38+
#### `Consumer`
39+
40+
```js
41+
<Consumer>
42+
{ value => { /* render something based on the context value */ } }
43+
</Consumer>
44+
```
45+
46+
A React component that subscribes to context changes.
47+
48+
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.
1749

1850
> Note:
1951
>
20-
> A [new, safe version of context](https://github.com/reactjs/rfcs/blob/master/text/0002-new-version-of-context.md) is under development for the upcoming 16.3 release.
52+
> For more information about this pattern, see [render props](/docs/render-props.html).
53+
54+
### Typical Usage
2155

56+
`embed:context/theme-detailed-theme-context.js`
2257

23-
## Why Not To Use Context
58+
`embed:context/theme-detailed-themed-button.js`
2459

25-
The vast majority of applications do not need to use context.
60+
`embed:context/theme-detailed-app.js`
2661

27-
If you want your application to be stable, don't use context. It is an experimental API and it is likely to break in future releases of React.
2862

29-
If you aren't familiar with state management libraries like [Redux](https://github.com/reactjs/redux) or [MobX](https://github.com/mobxjs/mobx), don't use context. For many practical applications, these libraries and their React bindings are a good choice for managing state that is relevant to many components. It is far more likely that Redux is the right solution to your problem than that context is the right solution.
63+
## Experimental API (Deprecated in React 16.3)
3064

31-
If you're still learning React, don't use context. There is usually a better way to implement functionality just using props and state.
65+
> The old experimental 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.
3266
33-
If you insist on using context despite these warnings, try to isolate your use of context to a small area and avoid using the context API directly when possible so that it's easier to upgrade when the API changes.
67+
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.
3468

35-
## How To Use Context
69+
### How To Use Context
70+
71+
> This section documents a deprecated API
3672
3773
Suppose you have a structure like:
3874

@@ -119,7 +155,15 @@ By adding `childContextTypes` and `getChildContext` to `MessageList` (the contex
119155

120156
If `contextTypes` is not defined, then `context` will be an empty object.
121157

122-
## Parent-Child Coupling
158+
> Note:
159+
>
160+
> `React.PropTypes` has moved into a different package since React v15.5. Please use [the `prop-types` library instead](https://www.npmjs.com/package/prop-types) to define `contextTypes`.
161+
>
162+
> We provide [a codemod script](/blog/2017/04/07/react-v15.5.0.html#migrating-from-react.proptypes) to automate the conversion.
163+
164+
### Parent-Child Coupling
165+
166+
> This section documents a deprecated API
123167
124168
Context can also let you build an API where parents and children communicate. For example, one library that works this way is [React Router V4](https://reacttraining.com/react-router):
125169

@@ -149,7 +193,9 @@ By passing down some information from the `Router` component, each `Link` and `R
149193

150194
Before you build components with an API similar to this, consider if there are cleaner alternatives. For example, you can pass entire React components as props if you'd like to.
151195

152-
## Referencing Context in Lifecycle Methods
196+
### Referencing Context in Lifecycle Methods
197+
198+
> This section documents a deprecated API
153199
154200
If `contextTypes` is defined within a component, the following [lifecycle methods](/docs/react-component.html#the-component-lifecycle) will receive an additional parameter, the `context` object:
155201

@@ -162,7 +208,9 @@ If `contextTypes` is defined within a component, the following [lifecycle method
162208
>
163209
> As of React 16, `componentDidUpdate` no longer receives `prevContext`.
164210
165-
## Referencing Context in Stateless Functional Components
211+
### Referencing Context in Stateless Functional Components
212+
213+
> This section documents a deprecated API
166214
167215
Stateless functional components are also able to reference `context` if `contextTypes` is defined as a property of the function. The following code shows a `Button` component written as a stateless functional component.
168216

@@ -177,7 +225,9 @@ const Button = ({children}, context) =>
177225
Button.contextTypes = {color: PropTypes.string};
178226
```
179227

180-
## Updating Context
228+
### Updating Context
229+
230+
> This section documents a deprecated API
181231
182232
Don't do it.
183233

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import ThemeContext from './theme-context';
2+
import ThemedButton from './button';
3+
4+
class App extends React.Component {
5+
state = {
6+
theme: {
7+
highlight: 'blue',
8+
accent: 'purple',
9+
},
10+
};
11+
12+
changeHighlightColor = () => {
13+
const colors = ['red', 'blue', 'green'];
14+
const randomColor =
15+
colors[Math.floor(Math.random() * 3)];
16+
this.setState({
17+
theme: {
18+
...this.state.theme,
19+
highlight: randomColor,
20+
},
21+
});
22+
};
23+
24+
render() {
25+
return (
26+
<ThemeContext.Provider value={this.state.theme}>
27+
<div>
28+
<ThemedButton onClick={this.changeHighlightColor}>
29+
Change Theme
30+
</ThemedButton>
31+
</div>
32+
</ThemeContext.Provider>
33+
);
34+
}
35+
}
36+
37+
ReactDOM.render(<App />, document.root);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const defaultTheme = {highlight: 'blue', accent: 'purple'};
2+
3+
const ThemeContext = React.createContext(defaultTheme);
4+
5+
export default ThemeContext;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import ThemeContext from './theme-context';
2+
3+
class ThemedButton extends React.Component {
4+
render() {
5+
return (
6+
<ThemeContext.Consumer>
7+
{theme => (
8+
<button
9+
{...this.props}
10+
style={{backgroundColor: theme.highlight}}
11+
/>
12+
)}
13+
</ThemeContext.Consumer>
14+
);
15+
}
16+
}
17+
18+
export default ThemedButton;

examples/context/theme-example.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const defaultTheme = 'light';
2+
// highlight-next-line
3+
const ThemeContext = React.createContext(defaultTheme);
4+
5+
class ThemeProvider extends React.Component {
6+
state = {theme: 'light'};
7+
8+
render() {
9+
// highlight-range{2-4}
10+
return (
11+
<ThemeContext.Provider value={this.state.theme}>
12+
{this.props.children}
13+
</ThemeContext.Provider>
14+
);
15+
}
16+
}
17+
18+
class ThemedButton extends React.Component {
19+
render() {
20+
//highlight-range{2-4}
21+
return (
22+
<ThemeContext.Consumer>
23+
{theme => <Button theme={theme} />}
24+
</ThemeContext.Consumer>
25+
);
26+
}
27+
}
28+
29+
class App extends React.Component {
30+
// highlight-range{3}
31+
// highlight-range{5}
32+
// highlight-range{7}
33+
render() {
34+
return (
35+
<ThemeProvider>
36+
<SomeComponent>
37+
<ThemedButton />
38+
</SomeComponent>
39+
</ThemeProvider>
40+
);
41+
}
42+
}

0 commit comments

Comments
 (0)