Home > OS >  Equivalent of a yield take Redux Saga in Redux thunks
Equivalent of a yield take Redux Saga in Redux thunks

Time:11-20

I'm currently converting the sagas in the code base to thunks.

I know that Sagas' specific functions such yield put, yield call have a "direct translation" to thunks dispatch(...) and await fn....

I came across yield take which from what I understand takes a set of actions included in the store and instructs the middleware to wait for one of those specified actions from the store and the result is an action object that gets dispatched?

What would be the "equivalent" if using Redux thunks?

Many thanks!

CodePudding user response:

sagas example:

export function* sample() {
    try {
        const response = yield call(api.sample)
        yield put(setData(response.data))
    } catch (error) {
        yield put(setError(error))
    }
}

export function* sampleSaga() {
yield takeEvery( YOUR_TYPE, sample)

}

if you wanna change it to redux thunk , you can do it:

 export function sample(){
   return (dispatch, getState) => {
     api.sample()
     .then((response)=> dispatch(setData(response.data)))
     .catch(error => Promise.reject(error))
  }
 }
  • Related