Skip to content

Commit f7cf140

Browse files
authored
Tweak context docs (#766)
1 parent b6c0a51 commit f7cf140

6 files changed

Lines changed: 68 additions & 54 deletions

examples/context/forwarding-refs-fancy-button.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class FancyButton extends React.Component {
88

99
// Use context to pass the current "theme" to FancyButton.
1010
// Use forwardRef to pass refs to FancyButton as well.
11-
// highlight-range{1,3}
11+
// highlight-range{1,4}
1212
export default React.forwardRef((props, ref) => (
1313
<ThemeContext.Consumer>
1414
{theme => (
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
function Button({theme, ...rest}) {
2-
// highlight-next-line
32
return <button className={theme} {...rest} />;
43
}
54

6-
// highlight-next-line
75
const ThemedButton = withTheme(Button);
Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
function ThemedButton(props) {
2-
//highlight-range{1}
3-
return <Button theme={props.theme} />;
1+
class App extends React.Component {
2+
render() {
3+
return <Toolbar theme="dark" />;
4+
}
45
}
56

6-
// An intermediate component
77
function Toolbar(props) {
8-
// highlight-range{1-2,5}
9-
// The Toolbar component must take an extra theme prop
10-
// and pass it to the ThemedButton
8+
// highlight-range{1-4,7}
9+
// The Toolbar component must take an extra "theme" prop
10+
// and pass it to the ThemedButton. This can become painful
11+
// if every single button in the app needs to know the theme
12+
// because it would have to be passed through all components.
1113
return (
1214
<div>
1315
<ThemedButton theme={props.theme} />
1416
</div>
1517
);
1618
}
1719

18-
class App extends React.Component {
19-
render() {
20-
// highlight-range{1}
21-
return <Toolbar theme="dark" />;
22-
}
20+
function ThemedButton(props) {
21+
return <Button theme={props.theme} />;
2322
}
Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
1-
// Create a theme context, defaulting to light theme
2-
// highlight-next-line
1+
// highlight-range{1-4}
2+
// Context lets us pass a value deep into the component tree
3+
// without explicitly threading it through every component.
4+
// Create a context for the current theme (with "light" as the default).
35
const ThemeContext = React.createContext('light');
46

5-
function ThemedButton(props) {
6-
// highlight-range{1,3-5}
7-
// The ThemedButton receives the theme from context
8-
return (
9-
<ThemeContext.Consumer>
10-
{theme => <Button {...props} theme={theme} />}
11-
</ThemeContext.Consumer>
12-
);
7+
class App extends React.Component {
8+
render() {
9+
// highlight-range{1-3,5}
10+
// Use a Provider to pass the current theme to the tree below.
11+
// Any component can read it, no matter how deep it is.
12+
// In this example, we're passing "dark" as the current value.
13+
return (
14+
<ThemeContext.Provider value="dark">
15+
<Toolbar />
16+
</ThemeContext.Provider>
17+
);
18+
}
1319
}
1420

15-
// An intermediate component
21+
// highlight-range{1,2}
22+
// A component in the middle doesn't have to
23+
// pass the theme down explicitly anymore.
1624
function Toolbar(props) {
1725
return (
1826
<div>
@@ -21,13 +29,14 @@ function Toolbar(props) {
2129
);
2230
}
2331

24-
class App extends React.Component {
25-
render() {
26-
// highlight-range{2,4}
27-
return (
28-
<ThemeContext.Provider value="dark">
29-
<Toolbar />
30-
</ThemeContext.Provider>
31-
);
32-
}
32+
function ThemedButton(props) {
33+
// highlight-range{1-3,6}
34+
// Use a Consumer to read the current theme context.
35+
// React will find the closest theme Provider above and use its value.
36+
// In this example, the current theme is "dark".
37+
return (
38+
<ThemeContext.Consumer>
39+
{theme => <Button {...props} theme={theme} />}
40+
</ThemeContext.Consumer>
41+
);
3342
}
Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,9 @@
11
// Theme context, default to light theme
2-
// highlight-next-line
32
const ThemeContext = React.createContext('light');
43

54
// Signed-in user context
6-
// highlight-next-line
75
const UserContext = React.createContext();
86

9-
// An intermediate component that depends on both contexts
10-
function Toolbar(props) {
11-
// highlight-range{2-10}
12-
return (
13-
<ThemeContext.Consumer>
14-
{theme => (
15-
<UserContext.Consumer>
16-
{user => (
17-
<ProfilePage user={user} theme={theme} />
18-
)}
19-
</UserContext.Consumer>
20-
)}
21-
</ThemeContext.Consumer>
22-
);
23-
}
24-
257
class App extends React.Component {
268
render() {
279
const {signedInUser, theme} = this.props;
@@ -31,9 +13,34 @@ class App extends React.Component {
3113
return (
3214
<ThemeContext.Provider value={theme}>
3315
<UserContext.Provider value={signedInUser}>
34-
<Toolbar />
16+
<Layout />
3517
</UserContext.Provider>
3618
</ThemeContext.Provider>
3719
);
3820
}
3921
}
22+
23+
function Layout() {
24+
return (
25+
<div>
26+
<Sidebar />
27+
<Content />
28+
</div>
29+
);
30+
}
31+
32+
// A component may consume multiple contexts
33+
function Content() {
34+
// highlight-range{2-10}
35+
return (
36+
<ThemeContext.Consumer>
37+
{theme => (
38+
<UserContext.Consumer>
39+
{user => (
40+
<ProfilePage user={user} theme={theme} />
41+
)}
42+
</UserContext.Consumer>
43+
)}
44+
</ThemeContext.Consumer>
45+
);
46+
}

examples/context/reference-caveats-solution.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class App extends React.Component {
22
constructor(props) {
3+
super(props);
34
// highlight-range{2}
45
this.state = {
56
value: {something: 'something'},

0 commit comments

Comments
 (0)