Dva: setup中如何获取 全局的state?

Created on 26 Apr 2017  ·  7Comments  ·  Source: dvajs/dva

subscriptions: {
setup({ dispatch, history }) {
history.listen(location => {});
});
}

setup中如何获取 全局的state?

Most helpful comment

  effects: {
    *getOwnerTasks({}, { call, put, select }){
      const user = yield select(state => state.app.user);
      const query = yield select(state => state.Home.query);
      const payload = {
        userId: user.userId,
        name: query.name,
        orderBy: query.orderBy,
      }
      const res = yield call(WorkflowService.API_GetOwnerTasks, payload);
      if (res.data.success) {
        yield put({
          type: 'getOwnerTasksReducer',
          payload: res.data.data,
        })
      }
    },
  },
  subscriptions: {
    setup({ dispatch, history }){
      history.listen(({ pathname }) => {
        if (pathname==='/home' || pathname==='/') {
          if (window.sessionStorage.login==="true") {
            dispatch({ type: 'getOwnerTasks' });
          }
        }
      })
    }
  },

给你个参考,我是这样写的

All 7 comments

这个还真不知道。
你的场景是什么,不能通过 url 把你需要的参数传过来吗?

其实就是间接获取啊,setup中dispatch

dispatch({
type: 'yourType'
})
在effects中,使用select, 
yield select(state => state)
  effects: {
    *getOwnerTasks({}, { call, put, select }){
      const user = yield select(state => state.app.user);
      const query = yield select(state => state.Home.query);
      const payload = {
        userId: user.userId,
        name: query.name,
        orderBy: query.orderBy,
      }
      const res = yield call(WorkflowService.API_GetOwnerTasks, payload);
      if (res.data.success) {
        yield put({
          type: 'getOwnerTasksReducer',
          payload: res.data.data,
        })
      }
    },
  },
  subscriptions: {
    setup({ dispatch, history }){
      history.listen(({ pathname }) => {
        if (pathname==='/home' || pathname==='/') {
          if (window.sessionStorage.login==="true") {
            dispatch({ type: 'getOwnerTasks' });
          }
        }
      })
    }
  },

给你个参考,我是这样写的

楼上说的没错,在subscriptions里dispatch

其实就是间接获取啊,setup中dispatch

dispatch({
type: 'yourType'
})
在effects中,使用select, 
yield select(state => state)

举个例子,我的场景是在 subscriptions 中判断当前 state 中的某个值是否存在,如果存在,那么我需要创建 WebSocket ,而在 effects 中是不能创建 WebSocket 的,于是就不知道怎么实现了

  effects: {
    *getOwnerTasks({}, { call, put, select }){
      const user = yield select(state => state.app.user);
      const query = yield select(state => state.Home.query);
      const payload = {
        userId: user.userId,
        name: query.name,
        orderBy: query.orderBy,
      }
      const res = yield call(WorkflowService.API_GetOwnerTasks, payload);
      if (res.data.success) {
        yield put({
          type: 'getOwnerTasksReducer',
          payload: res.data.data,
        })
      }
    },
  },
  subscriptions: {
    setup({ dispatch, history }){
      history.listen(({ pathname }) => {
        if (pathname==='/home' || pathname==='/') {
          if (window.sessionStorage.login==="true") {
            dispatch({ type: 'getOwnerTasks' });
          }
        }
      })
    }
  },

给你个参考,我是这样写的

没有解决楼主的问题吧。

其实就是间接获取啊,setup中dispatch

dispatch({
type: 'yourType'
})
在effects中,使用select, 
yield select(state => state)

举个例子,我的场景是在 subscriptions 中判断当前 state 中的某个值是否存在,如果存在,那么我需要创建 WebSocket ,而在 effects 中是不能创建 WebSocket 的,于是就不知道怎么实现了

请问如何解决的?

Was this page helpful?
0 / 5 - 0 ratings