Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/context/forwarding-refs-fancy-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class FancyButton extends React.Component {

// Use context to pass the current "theme" to FancyButton.
// Use forwardRef to pass refs to FancyButton as well.
// highlight-range{1,3}
// highlight-range{1,4}
export default React.forwardRef((props, ref) => (
<ThemeContext.Consumer>
{theme => (
Expand Down
2 changes: 0 additions & 2 deletions examples/context/higher-order-component-usage.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
function Button({theme, ...rest}) {
// highlight-next-line
return <button className={theme} {...rest} />;
}

// highlight-next-line
const ThemedButton = withTheme(Button);
23 changes: 11 additions & 12 deletions examples/context/motivation-problem.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
function ThemedButton(props) {
//highlight-range{1}
return <Button theme={props.theme} />;
class App extends React.Component {
render() {
return <Toolbar theme="dark" />;
}
}

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

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

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

// An intermediate component
// highlight-range{1,2}
// A component in the middle doesn't have to
// pass the theme down explicitly anymore.
function Toolbar(props) {
return (
<div>
Expand All @@ -21,13 +29,14 @@ function Toolbar(props) {
);
}

class App extends React.Component {
render() {
// highlight-range{2,4}
return (
<ThemeContext.Provider value="dark">
<Toolbar />
</ThemeContext.Provider>
);
}
function ThemedButton(props) {
// highlight-range{1-3,6}
// Use a Consumer to read the current theme context.
// React will find the closest theme Provider above and use its value.
// In this example, the current theme is "dark".
return (
<ThemeContext.Consumer>
{theme => <Button {...props} theme={theme} />}
</ThemeContext.Consumer>
);
}
45 changes: 26 additions & 19 deletions examples/context/multiple-contexts.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,9 @@
// Theme context, default to light theme
// highlight-next-line
const ThemeContext = React.createContext('light');

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

// An intermediate component that depends on both contexts
function Toolbar(props) {
// highlight-range{2-10}
return (
<ThemeContext.Consumer>
{theme => (
<UserContext.Consumer>
{user => (
<ProfilePage user={user} theme={theme} />
)}
</UserContext.Consumer>
)}
</ThemeContext.Consumer>
);
}

class App extends React.Component {
render() {
const {signedInUser, theme} = this.props;
Expand All @@ -31,9 +13,34 @@ class App extends React.Component {
return (
<ThemeContext.Provider value={theme}>
<UserContext.Provider value={signedInUser}>
<Toolbar />
<Layout />
</UserContext.Provider>
</ThemeContext.Provider>
);
}
}

function Layout() {
return (
<div>
<Sidebar />
<Content />
</div>
);
}

// A component may consume multiple contexts
function Content() {
// highlight-range{2-10}
return (
<ThemeContext.Consumer>
{theme => (
<UserContext.Consumer>
{user => (
<ProfilePage user={user} theme={theme} />
)}
</UserContext.Consumer>
)}
</ThemeContext.Consumer>
);
}
1 change: 1 addition & 0 deletions examples/context/reference-caveats-solution.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class App extends React.Component {
constructor(props) {
super(props);
// highlight-range{2}
this.state = {
value: {something: 'something'},
Expand Down