Skip to content

Instantly share code, notes, and snippets.

@srkirkland
Created November 29, 2017 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save srkirkland/876a0cf3f9fe920d17058607257080eb to your computer and use it in GitHub Desktop.
Save srkirkland/876a0cf3f9fe920d17058607257080eb to your computer and use it in GitHub Desktop.
notes about how to enable context in TSX
// Main Type of the context
export interface ProviderContext {
name: String;
}
// Provider
export default class App extends React.Component<{}, {}> {
static childContextTypes = {
name: PropTypes.string
}
getChildContext() { // define context here
return {
name: 'TEST'
}
}
render() {
return this.props.children;
}
}
// Consumer
export default class PersonContainer extends React.Component<IProps, {}> {
static contextTypes = {
name: PropTypes.string
};
context: ProviderContext;
componentDidMount() {
console.log(this.context.name);
}
public render() {
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment