Skip to content

Instantly share code, notes, and snippets.

@becca-bailey
Created November 15, 2018 03:15
Show Gist options
  • Save becca-bailey/c8539d869b3ef4553381356734ea670e to your computer and use it in GitHub Desktop.
Save becca-bailey/c8539d869b3ef4553381356734ea670e to your computer and use it in GitHub Desktop.
const AlertContext = React.createContext<IAlertContext>({
alert: undefined,
showAlert: (alert) => {},
});
// Here we are creating a context with default values
class AlertProvider extends React.Component {
state = {
alert: undefined
};
render() {
const { children } = this.props;
return (
<AlertContext.Provider
value={{
...this.state,
showAlert: this.showAlert,
}}
>
{children}
</AlertContext.Provider>
);
}
private showAlert = alert => {
this.setState({
alert
});
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment