Skip to content

Instantly share code, notes, and snippets.

View hg-pyun's full-sized avatar
🎯
Focusing

Haegul Pyun hg-pyun

🎯
Focusing
View GitHub Profile
@hg-pyun
hg-pyun / medium_react_v16.3.0_01.js
Created April 4, 2018 13:46
react v16.3.0 medium 01
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>
@hg-pyun
hg-pyun / medium_react_v16.3.0_05.js
Created April 4, 2018 14:05
react v16.3.0 medium 05
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}
@hg-pyun
hg-pyun / medium_react_v16.3.0_03.js
Created April 4, 2018 14:04
react v16.3.0 medium 03
function withTheme(Component) {
return function ThemedComponent(props) {
return (
<ThemeContext.Consumer>
{theme => <Component {...props} theme={theme} />}
</ThemeContext.Consumer>
);
};
}
// 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({
<Mouse children={mouse => (
<p>The mouse position is {mouse.x}, {mouse.y}</p>
)}/>
<Mouse>
{mouse => (
<p>The mouse position is {mouse.x}, {mouse.y}</p>
)}
</Mouse>
<DataProvider render={data => (
<h1>Hello {data.target}</h1>
)}/>
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>
class Mouse extends React.PureComponent {
// Same implementation as above...
}
class MouseTracker extends React.Component {
render() {
return (
<div>
<h1>Move the mouse around!</h1>
Mouse.propTypes = {
children: PropTypes.func.isRequired
};