
正文
[MST] Loading Data from the Server using lifecycle hook
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
Let's stop hardcoding our initial state and fetch it from the server instead.
In this lesson you will learn:
- Set up basic data fetching
-
Leverage the
afterCreatelifecycle hook to automatically run any setup logic a model instance needs to do after creation
export const Group = types
.model({
users: types.map(User)
})
.actions(self => ({
// lifecycle hook
afterCreate() {
self.load()
},
load: flow(function* load() {
const response = yield window.fetch(`http://localhost:3001/users`)
applySnapshot(self.users, yield response.json())
})
}))







