reduxreact-reduxredux-saga

How can I create takeOne effect creator in Redux Saga?


I want a Redux Saga effect creator that only takes the first dispatched action of a pattern and ignores the rest. How can I create it? For example here Saga docs explains how takeEvery is created. I would have some thing like this in the Saga function that I run in my app:

function* mySaga() {
   takeOne("one_time_action", someSaga);
}

Solution

  • Use yield take() to wait for patternOrChannel and then fork the saga you want to call. Essentially it's takeEvery without the loop:

    const takeOne = (patternOrChannel, saga, ...args) => fork(function*() {
      const action = yield take(patternOrChannel)
      yield fork(saga, ...args.concat(action))
    })