Created
April 7, 2017 13:13
-
-
Save shalkam/8604f256201652a24987b9af454a29fe to your computer and use it in GitHub Desktop.
Simple react context example
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 Parent extends Component { | |
getChildContext() { | |
return {componentID: "123456"}; | |
} | |
render() { | |
return <Child1 /> | |
} | |
} | |
Parent.childContextTypes = { | |
componentID: React.PropTypes.string | |
}; | |
class Child1 extends Component { | |
render() { | |
return <Child2 /> | |
} | |
} | |
class Child2 extends Component { | |
render() { | |
return <div>{this.context.componentID}</div> | |
} | |
} | |
Child2.contextTypes = { | |
componentID: React.PropTypes.string | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment