Dva: redux-saga fork 问题

Created on 21 Apr 2017  ·  8Comments  ·  Source: dvajs/dva

这么写fork找不到a和b函数,是我写得有问题么?

export default {
  namespace: 'app',
  state: {},
  reducers: {},
  effects: {
    *a(action, { call, put }) {....},
    *b(action, { call, put }) {....},
    *test(action, { fork }) {
      yield fork(a);
      yield fork(b);
    }
  }
}

All 8 comments

这样肯定找不到的,如果你要fork两个东西的话,感觉写出去变成单独的函数比较好?

function *a() {}
function *b() {}

写在这个model的结构外面去

@xufei 写到外面肯定可以,可是我要在a和b函数中调用call或put不就不方便了么

@xufei 我想达到得效果就是在test函数中发起两个异步请求获取数据,拿到数据后更新redux中得state,这两个请求包括后续得更新redux state操作是互相独立并行执行得,如果不用fork的话还有其它写法么?

yield put({type: 'a'})
yield put({type: 'b'})

>
yield put({type: 'a'})
yield put({type: 'b'})

Are those dispatched asynchronously?

@megawac
The "put" dispatch action to middleware, and middleware catch the action that type is "effect", then return promise. so it is asynchronously.

使用fork 可以取消这个任务

*login({ payload: { account, smscode } }, { fork }) {
  const task = fork(loginbySmscode, { account, smscode })
  yield take(['login', 'logout'])
  task.cancel()
}

改成put来调用 怎么取消这个任务?

感觉dva这样封装, 很难进行fork.

Was this page helpful?
0 / 5 - 0 ratings