Last active
September 30, 2019 09:27
-
-
Save Palatnyi/618fb9e2c9f6b42dd93dfa575cfb4a24 to your computer and use it in GitHub Desktop.
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 App extends Component { | |
static childContextTypes = { | |
counter: PropTypes.object | |
} | |
constructor(props) { | |
super(props); | |
this.state = { | |
count: 0, | |
increment: this.increment | |
}; | |
} | |
increment = () => this.setState({count: this.state.count + 1}); | |
getChildContext() { | |
return { | |
counter: this.state | |
} | |
} | |
render() { | |
return ( | |
<div className="App"> | |
<header className="App-header"> | |
Counter | |
</header> | |
{this.props.children} | |
</div>); | |
} | |
} | |
class Layout extends Component { | |
render() { | |
return ( | |
<div> | |
<Title /> | |
<Increment/> | |
</div> | |
); | |
} | |
} | |
class Increment extends React.Component { | |
static contextTypes = { | |
counter: PropTypes.object, | |
} | |
render() { | |
return <button onClick={this.context.counter.increment}> Inctement me</button> | |
} | |
} | |
class Title extends React.Component { | |
static contextTypes = { | |
counter: PropTypes.object, | |
} | |
render = () => <h1>{this.context.counter.count}</h1> | |
} | |
export default function () { | |
return ( | |
<App> | |
<Layout/> | |
</App> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment