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
class MyComponent extends React.Component { | |
constructor(props) { | |
super(props); | |
this.inputRef = React.createRef(); | |
} | |
render() { | |
return <input type="text" ref={this.inputRef} />; | |
} |
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 FancyButton extends React.Component { | |
buttonRef = React.createRef(); | |
focus() { | |
this.buttonRef.current.focus(); | |
} | |
render() { | |
const {label, theme, ...rest} = this.props; | |
return ( |
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
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
<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 { | |
constructor(props) { | |
super(props); | |
this.handleMouseMove = this.handleMouseMove.bind(this); | |
this.state = { x: 0, y: 0 }; | |
} | |
handleMouseMove(event) { | |
this.setState({ | |
x: event.clientX, |
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
class Cat extends React.Component { | |
render() { | |
const mouse = this.props.mouse; | |
return ( | |
<img src="/cat.jpg" style={{ position: 'absolute', left: mouse.x, top: mouse.y }} /> | |
); | |
} | |
} | |
class MouseWithCat extends React.Component { |
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 Cat extends React.Component { | |
render() { | |
const mouse = this.props.mouse; | |
return ( | |
<img src="/cat.jpg" style={{ position: 'absolute', left: mouse.x, top: mouse.y }} /> | |
); | |
} | |
} | |
class Mouse extends React.Component { |
OlderNewer