react-mirror initiates an asynchronous request

mirror is Alibaba's open source state management tool.

Instructions:

https://github.com/mirrorjs/mirror/blob/master/README_zh.md

Asynchronous request:

The problem encountered by beginners: every asynchronous request can be triggered, and the return value is correct, but changing the state in Promise.then cannot trigger the view update. After inspection, the state cannot be changed directly in the asynchronous callback or promise then, and it needs to be called reducers.

code show as below

mirror.model({
  name: 'appStore',
  initialState: {
    todoList: {},
    chart: {},
    news: {},
    siderLink: {}
  },
  reducers: {
    save(state, data) {
      return { ...state, ...data }
    }
  },
  effects: {
    async initTodo() {
      const response = await axios.get(URILIST[0]);
      actions.appStore.save({
        todoList: response.data
      });
    }
  }
})

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324730373&siteId=291194637