Skip to content

Instantly share code, notes, and snippets.

@jossmac
Last active November 1, 2018 03:02
Show Gist options
  • Save jossmac/507d12329e30bc50b7b2f36de229ac23 to your computer and use it in GitHub Desktop.
Save jossmac/507d12329e30bc50b7b2f36de229ac23 to your computer and use it in GitHub Desktop.
Pseudo code for a provider component
import React from 'react';
import Renderer from './Renderer';
export const ToastContext = React.createContext();
export class ToastProvider extends React.Component {
state = { toasts: [] }
add = (content) => {
this.setState(...);
}
remove = (id) => {
this.setState(...);
}
render() {
const context = {
add: this.add,
remove: this.remove,
}
return (
<ToastContext.Provider value={context}>
{this.props.children}
<Renderer toasts={this.state.toasts} />
</ToastContext.Provider>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment