Skip to content

Instantly share code, notes, and snippets.

@alexvcasillas
Created August 22, 2017 07:21
Show Gist options
  • Save alexvcasillas/d94b72801511df2afcd2aa98dd4bdc42 to your computer and use it in GitHub Desktop.
Save alexvcasillas/d94b72801511df2afcd2aa98dd4bdc42 to your computer and use it in GitHub Desktop.
MST Lifecycle Hooks
import { types } from 'mobx-state-tree';
const UserStore = types
.model('UserStore', {
id: types.optional(types.identifier(), 'user-id'),
name: types.optional(types.string, ''),
lastName: types.optional(types.string, ''),
email: types.optional(types.string, '')
})
.views(self => ({
get fullName() {
return `${self.name} ${self.lasName}`;
}
}))
.actions(self => ({
afterCreate() { // This won't get triggered after create
return 'User Store has been created, ready to fetch data now';
},
postCreate() {
return 'Store has been created';
},
setName(name) {
self.name = name;
},
setLastName(lastName) {
self.lastName = lastName;
},
setEmail(email) {
self.email = email;
}
}));
export default UserStore;
// At React App.js
const userStore = UserStore.create({
id: '1',
name: 'Alex',
lastName: 'Casillas',
email: 'alexvcasillas@gmail.com'
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment