Scenario: I want to redirect the user or show a warning based on success, error callbacks after sending the action.
Below is the abbreviated code for the task
this.props.actions.login(credentials) .then((success)=>redirectToHomePage) .catch((error)=>alertError);
because the submit action in redux-thunk returns Promise, it is easy to respond with a response.
But now I get my hands dirty on sound reduction and trying to figure out how I can achieve the same result as the code above. since Saga is running in a different thread, I cannot get the callback from the request above. so I just wanted to know how you guys do it. or what is the best way to handle callbacks when using the saga saga? the submit action is as follows:
this.props.actions.login (credentials);
and saga
function* login(action) { try { const state = yield select(); const token = state.authReducer.token; const response = yield call(API.login,action.params,token); yield put({type: ACTION_TYPES.LOGIN_SUCCESS, payload:response.data}); yield call(setItem,AUTH_STORAGE_KEY,response.data.api_token); } catch (error) { yield put({type: ACTION_TYPES.LOGIN_FAILURE, error}) } }
saga monitor
export function* loginMonitor() { yield takeLatest(ACTION_TYPES.LOGIN_REQUEST,login); }
action creator
function login(params) { return { type: ACTION_TYPES.LOGIN_REQUEST, params } }
reactjs redux redux-saga
Zal
source share