Created
September 10, 2015 21:54
-
-
Save VaheGrigorian/36af98b57349bdd4e2d6 to your computer and use it in GitHub Desktop.
React context with Typescript
This file contains hidden or 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
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