redux-saga比赛之间的“带”有效载荷条件

时间:2018-02-09 01:24:31

标签: redux-saga

我有

yield race([
  take('ACTION_ONE'),
  take('ACTION_TWO')
])

但是我想指定ACTION_ONE整理的条件是动作有效负载具有特定值。我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

可能你需要定义一个新的传奇来处理' ACTION_ONE'并把它放在比赛中。像这样:

function* actionOneSaga() {
  let notFound = true;
  while(notFound) {
    const action = yield take('ACTION_ONE');
    if (action.payload === 'SOME_VALUE') {
      doSomethingWithYourValue(action.payload);
      notFound = false;
    }
  }
}

然后你可以这样做:

yield race([
  take(actionOneSaga),
  take('ACTION_TWO')
])

答案 1 :(得分:0)

您可以在汇整中传递回调函数。

const { action1, action2 } = yield race({
  action1: take(action => action.type === 'ACTION_ONE' && action.value === value),
  action2: take(action => action.type === 'ACTION_TWO' && action.value === value),
})

if (action1) {
   console.log('action1 fired')
}

if (action2) {
   console.log('action2 fired')
}