Last active
April 9, 2019 19:51
-
-
Save vadimkorr/15a988cc632186e57c812f8c63fa9cee to your computer and use it in GitHub Desktop.
This file contains 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
import { observable, action, computed } from "mobx"; | |
export class NotificationsStore { | |
constructor(notifications) { | |
this.notifications = [...notifications]; | |
} | |
// Observers will be notified and react to changes | |
// in properties which have @observable decorator. | |
@observable | |
notifications = []; | |
// @action decorator should be used on functions | |
// that modify state. In a real app, this function could be called | |
// e.g. on SignalR event. | |
// Observers will be notified when new notification is created | |
@action | |
add(notification) { | |
this.notifications.push(notification); | |
} | |
// 'remove' function also modifies the state, | |
// that's why it should has @action decorator. | |
@action | |
remove(removedNotification) { | |
this.notifications = this.notifications.filter( | |
notification => notification.id !== removedNotification.id | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment