您的位置:首页 > Web前端

[MST] Loading Data from the Server using lifecycle hook

2018-01-30 03:40 411 查看
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
afterCreate
lifecycle 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())
})
}))
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐