码迷,mamicode.com
首页 > 其他好文 > 详细

[MST] Loading Data from the Server using lifecycle hook

时间:2018-01-30 12:31:59      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:fun   pre   using   function   stop   action   type   col   blog   

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())
        })
    }))

 

[MST] Loading Data from the Server using lifecycle hook

标签:fun   pre   using   function   stop   action   type   col   blog   

原文地址:https://www.cnblogs.com/Answer1215/p/8382679.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!