Created
August 22, 2017 07:21
-
-
Save alexvcasillas/d94b72801511df2afcd2aa98dd4bdc42 to your computer and use it in GitHub Desktop.
MST Lifecycle Hooks
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 { 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: '[email protected]' | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment