Skip to content

Instantly share code, notes, and snippets.

@VaheGrigorian
Created September 10, 2015 21:54
Show Gist options
  • Save VaheGrigorian/36af98b57349bdd4e2d6 to your computer and use it in GitHub Desktop.
Save VaheGrigorian/36af98b57349bdd4e2d6 to your computer and use it in GitHub Desktop.
React context with Typescript
ParentComponent extends React.Component<{}, {}>{
static childContextTypes:React.ValidationMap<any> = {
name: React.PropTypes.string.isRequired
}
constructor(props, context){
super(props, context);
}
getChildContext(){
return {
name: 'Vahe'
}
}
render(){
return <h1>Hello World<ChildComponent /></h1>;
}
}
class ChildComponent extends React.Component<{}, {}>{
context:{name: string}
static contextTypes: React.ValidationMap<any> = {
name: React.PropTypes.string.isRequired
}
constructor(props, context){
super(props, context);
}
render(){
return <p>I am {this.context.name}</p>;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment