Skip to content

Instantly share code, notes, and snippets.

@Palatnyi
Last active September 30, 2019 09:27
Show Gist options
  • Save Palatnyi/618fb9e2c9f6b42dd93dfa575cfb4a24 to your computer and use it in GitHub Desktop.
Save Palatnyi/618fb9e2c9f6b42dd93dfa575cfb4a24 to your computer and use it in GitHub Desktop.
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