5 changed files with 68 additions and 134 deletions
@ -1,34 +1,23 @@ |
|||||
class Button extends React.Component { |
const ThemedButton = props => { |
||||
render() { |
//highlight-range{1}
|
||||
return ( |
return <Button theme={props.theme} />; |
||||
<button style={{background: this.props.color}}> |
}; |
||||
{this.props.children} |
|
||||
</button> |
|
||||
); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
class Message extends React.Component { |
// An intermediate component
|
||||
render() { |
const Toolbar = props => { |
||||
// highlight-range{1-3}
|
// highlight-range{1-2,5}
|
||||
// The Message component must take `color` as as prop to pass it
|
// The Toolbar component must take an extra theme prop
|
||||
// to the Button. Using context, the Button could connect to the
|
// and pass it to the ThemedButton
|
||||
// color context on its own.
|
return ( |
||||
return ( |
<div> |
||||
<div> |
<ThemedButton theme={props.theme} /> |
||||
<p>{this.props.text}</p> |
</div> |
||||
<Button color={this.props.color}>Delete</Button> |
); |
||||
</div> |
}; |
||||
); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
class MessageList extends React.Component { |
class App extends React.Component { |
||||
render() { |
render() { |
||||
const color = 'purple'; |
// highlight-range{1}
|
||||
const children = this.props.messages.map(message => ( |
return <Toolbar theme="dark" />; |
||||
<Message text={message.text} color={color} /> |
|
||||
)); |
|
||||
return <div>{children}</div>; |
|
||||
} |
} |
||||
} |
} |
||||
|
@ -1,43 +1,31 @@ |
|||||
// highlight-range{1}
|
// Create a theme context, defaulting to light theme
|
||||
const ColorContext = React.createContext(); |
// highlight-next-line
|
||||
|
const ThemeContext = React.createContext('light'); |
||||
|
|
||||
class Button extends React.Component { |
// highlight-range{1,3-5}
|
||||
render() { |
// The ThemedButton receives the theme from context
|
||||
// highlight-range{2-8}
|
const ThemedButton = props => ( |
||||
return ( |
<ThemeContext.Consumer> |
||||
<ColorContext.Consumer> |
{theme => <Button theme={theme} />} |
||||
{color => ( |
</ThemeContext.Consumer> |
||||
<button style={{background: color}}> |
); |
||||
{this.props.children} |
|
||||
</button> |
|
||||
)} |
|
||||
</ColorContext.Consumer> |
|
||||
); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
class Message extends React.Component { |
// An intermediate component
|
||||
render() { |
const Toolbar = props => { |
||||
return ( |
return ( |
||||
<div> |
<div> |
||||
<p>{this.props.text}</p> |
<ThemedButton /> |
||||
<Button>Delete</Button> |
</div> |
||||
</div> |
); |
||||
); |
}; |
||||
} |
|
||||
} |
|
||||
|
|
||||
class MessageList extends React.Component { |
class App extends React.Component { |
||||
render() { |
render() { |
||||
const color = 'purple'; |
// highlight-range{2,4}
|
||||
const children = this.props.messages.map(message => ( |
|
||||
<Message text={message.text} /> |
|
||||
)); |
|
||||
// highlight-range{2-4}
|
|
||||
return ( |
return ( |
||||
<ColorContext.Provider value={color}> |
<ThemeContext.Provider value="dark"> |
||||
{children} |
<Toolbar /> |
||||
</ColorContext.Provider> |
</ThemeContext.Provider> |
||||
); |
); |
||||
} |
} |
||||
} |
} |
||||
|
@ -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 ( |
|
||||
<ThemeContext.Provider value={this.props.theme}> |
|
||||
{this.props.children} |
|
||||
</ThemeContext.Provider> |
|
||||
); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
class ThemedButton extends React.Component { |
|
||||
render() { |
|
||||
//highlight-range{2-4}
|
|
||||
return ( |
|
||||
<ThemeContext.Consumer> |
|
||||
{theme => <Button theme={theme} />} |
|
||||
</ThemeContext.Consumer> |
|
||||
); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
const SomeComponent = props => { |
|
||||
// The ThemedButton receives the theme from context;
|
|
||||
// SomeComponent does not need to know about it
|
|
||||
// highlight-range{3}
|
|
||||
return ( |
|
||||
<div> |
|
||||
<ThemedButton /> |
|
||||
</div> |
|
||||
); |
|
||||
}; |
|
||||
|
|
||||
class App extends React.Component { |
|
||||
render() { |
|
||||
// The ThemedButton button inside the ThemeProvider
|
|
||||
// uses the dark theme while the one outside uses the
|
|
||||
// default light theme
|
|
||||
// highlight-range{3-5,7}
|
|
||||
return ( |
|
||||
<div> |
|
||||
<ThemeProvider theme="dark"> |
|
||||
<SomeComponent /> |
|
||||
</ThemeProvider> |
|
||||
<div> |
|
||||
<ThemedButton /> |
|
||||
</div> |
|
||||
</div> |
|
||||
); |
|
||||
} |
|
||||
} |
|
Loading…
Reference in new issue