This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ThemeContext = React.createContext('light'); | |
class ThemeProvider extends React.Component { | |
state = {theme: 'light'}; | |
render() { | |
return ( | |
<ThemeContext.Provider value={this.state.theme}> | |
{this.props.children} | |
</ThemeContext.Provider> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function withTheme(Component) { | |
function ThemedComponent({forwardedRef, ...rest}) { | |
return ( | |
<ThemeContext.Consumer> | |
{theme => ( | |
// Assign the custom prop "forwardedRef" as a ref | |
<Component | |
{...rest} | |
ref={forwardedRef} | |
theme={theme} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function withTheme(Component) { | |
return function ThemedComponent(props) { | |
return ( | |
<ThemeContext.Consumer> | |
{theme => <Component {...props} theme={theme} />} | |
</ThemeContext.Consumer> | |
); | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The <Mouse> component encapsulates the behavior we need... | |
class Mouse extends React.Component { | |
constructor(props) { | |
super(props); | |
this.handleMouseMove = this.handleMouseMove.bind(this); | |
this.state = { x: 0, y: 0 }; | |
} | |
handleMouseMove(event) { | |
this.setState({ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Mouse children={mouse => ( | |
<p>The mouse position is {mouse.x}, {mouse.y}</p> | |
)}/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Mouse> | |
{mouse => ( | |
<p>The mouse position is {mouse.x}, {mouse.y}</p> | |
)} | |
</Mouse> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<DataProvider render={data => ( | |
<h1>Hello {data.target}</h1> | |
)}/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MouseTracker extends React.Component { | |
// Defined as an instance method, `this.renderTheCat` always | |
// refers to *same* function when we use it in render | |
renderTheCat(mouse) { | |
return <Cat mouse={mouse} />; | |
} | |
render() { | |
return ( | |
<div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Mouse extends React.PureComponent { | |
// Same implementation as above... | |
} | |
class MouseTracker extends React.Component { | |
render() { | |
return ( | |
<div> | |
<h1>Move the mouse around!</h1> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Mouse.propTypes = { | |
children: PropTypes.func.isRequired | |
}; |
OlderNewer