Search code examples
reactjsreduxredux-sagaredux-toolkit

some question about redux-toolkit redux-saga


I have a project that use redux-toolkit and redux-saga it is working fine now. but now i have some question do I need to use redux-saga.

  1. one of most feature I use redux-saga because it has a side effect. if I dispatch an action on redux after the reducer executes It can execute another saga function.
  yield all([
    takeEvery<any>(boardAcctions.addWidget.type, updateWidgetUI),
    takeEvery<any>(boardAcctions.moveWidget.type, updateWidgetUI),
    takeEvery<any>(boardAcctions.updateWidgetConfig.type, updateWidgetUI),
    takeEvery<any>(boardAcctions.removeWidget.type, updateWidgetUI),
  ])

  1. on saga function you can dispatch another action so if you have multiple actions need to update it will be very easy with saga.
    yield put(loginActions.updateUser(userdata))
    yield put(dashboardAction.updateBoard(dashboarData))

but on redux-toolkit with function createAsyncThunk(). I can't dispatch another action. even on extra reduces it just only a reducer and I think dispatch another action inside that is not illegal

how do I do that without redux-saga?

Thank


Solution

  • Of course you can dispatch other actions from within createAsyncThunk.

    import { createAsyncThunk } from '@reduxjs/toolkit'
    
    const fetchUserById = createAsyncThunk(
      'users/fetchByIdStatus',
      async (userId, { dispatch }) => {
    
        dispatch(someOtherAction())
    
      }
    )