Skip to content

Instantly share code, notes, and snippets.

@jesty
Last active April 4, 2018 23:04
Show Gist options
  • Save jesty/7e79e5d9c6319f9230713ef0fb041e24 to your computer and use it in GitHub Desktop.
Save jesty/7e79e5d9c6319f9230713ef0fb041e24 to your computer and use it in GitHub Desktop.
Just for a Medium post
import React, { Component } from 'react';
import {MainContext} from './Context';
class Child extends Component {
happy = main => main.onChangeStatus("happy :D")
sad = main => main.onChangeStatus("sad :(")
render() {
return (
<MainContext.Consumer>
{main => <div>
<button onClick={() => this.happy(main)}>Happy</button>
<button onClick={() => this.sad(main)}>Sad</button>
</div>}
</MainContext.Consumer>
);
}
}
export default Child;
import React from 'react';
export const MainContext = React.createContext('main');
import React, { Component } from 'react';
import Parent from './Parent';
import {MainContext} from './Context';
class Grandparent extends Component {
state = {status: "..."};
handleChangeStatus = (status) => this.setState({status});
render() {
let {status} = this.state;
return (
<MainContext.Provider value={{onChangeStatus: this.handleChangeStatus}}>
I am {status}
<Parent />
</MainContext.Provider>
);
}
}
export default Grandparent;
import React, { Component } from 'react';
import Child from './Child';
class Parent extends Component {
render() {
return (
<Child />
);
}
}
export default Parent;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment