Created
November 29, 2017 18:15
-
-
Save srkirkland/876a0cf3f9fe920d17058607257080eb to your computer and use it in GitHub Desktop.
notes about how to enable context in TSX
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
// 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