Last active
April 4, 2018 23:04
-
-
Save jesty/7e79e5d9c6319f9230713ef0fb041e24 to your computer and use it in GitHub Desktop.
Just for a Medium post
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
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; |
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
import React from 'react'; | |
export const MainContext = React.createContext('main'); |
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
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; |
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
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