-1
I’m having a problem with Redux Saga. The request is coming in the action and the Return but does not go through Saga. I’m starting in Saga so I can’t identify what the problem is
Action:
export const login = (data) => ({ type: 'LOGIN', data })
Reduce:
export default function login(state = INITIAL_STATE, action) {
switch(action.type) {
case 'LOGIN':
return {...state, loading: true};
case 'SUCCESS_LOGIN':
return {...state, data: action.data, loading: false, error: false};
case 'FAILURE_LOGIN':
return {data: {}, loading: false, error: true};
default:
return state;
}
}
Saga:
import { takeEvery, put, takeLatest, call } from 'redux-saga/effects';
import AXIOS from '../../config/axios';
async function apiLogin(data) {
const response = await AXIOS.post('/login', data);
localStorage.setItem('token', response.data.token);
return response;
}
function* asyncLogin() {
try {
const response = yield call(apiLogin)
yield put({ type: 'SUCCESS_LOGIN', payload: { data: response } })
} catch( err ) {
yield put({ type: 'FAILURE_LOGIN' });
console.log(err)
}
}
export default function* root() {
yield [
takeLatest('LOGIN', asyncLogin)
];
}
Store:
import { createStore, applyMiddleware } from 'redux';
import createSagaMiddleware from 'redux-saga';
import { Reducers } from '../reducer';
import rootSaga from '../saga';
const sagaMiddleware = createSagaMiddleware();
const store = createStore(Reducers, applyMiddleware(sagaMiddleware));
sagaMiddleware.run(rootSaga);
export default store;