-
Notifications
You must be signed in to change notification settings - Fork 248
Open
Labels
Description
Rather than worrying about how to integrate with Redux/Flux/Reflux/etc, I've found the best way to integrate this with a single page app is to use context.
Given this structure in my App container component's render:
<div >
<Header />
<NotificationSystem ref={ref => this.notifications = ref} />
{children}
<Footer />
</div>
Side note: you can pass a function to ref rather than setting it in a lifecycle method like "componentDidMount" (as shown in the example code).
Then you put this in the App component...
static childContextTypes = {
addNotification: PropTypes.func,
};
getChildContext() {
return {
addNotification: this.addNotification.bind(this),
};
}
addNotification(notification) {
this.notifications.addNotification(notification);
}
Then, when any child component that is rendered inside the App component declares a context type like so:
static contextTypes = {
addNotification: PropTypes.func.isRequired,
};
Then you can call it like this in that child component:
someFunction(){
this.context.addNotification({
title: 'New Message',
message: "You've received a message from the server!",
level: 'warning',
dismissable: false,
});
}
Using context is easier, IMO, than using a redux/reflux/flux because this component was made to have a function called rather than have its state set, which context is much better for.
srisonti, krzkaczor and fetzigfetzig