Search code examples
redux-saga

How can I get data from an action?


For example there is an Action:

export const loginSuccessAction = (user: UserInterface) => {
  return (dispatch: Dispatch<ActionInterface>) => {
    dispatch({ type: ActionTypes.LOGIN_SUCCESS, payload: user });
  };
};

It is possible being in Watcher to get (user: UserInterface) from loginSuccessAction?

Thank You very much for Your answer


Solution

  • Yes, each of the methods for taking an action will give you the action object, and since you've put the user on the payload property, you can use that. For example with takeEvery:

    function* watchLogin() {
       yield takeEvery(ActionTypes.LOGIN_SUCCESS, loginSuccessSaga);
    }
    
    function* loginSuccessSaga(action) {
      console.log('user: ', action.payload);
    }
    

    Or with take:

    function* someSaga() {
      const action = yield take(ActionTypes.LOGIN_SUCCESS);
      console.log('user: ', action.payload);
    }