Making API calls
Validating the request
Logging
Crash reporting
Routing
Extending the request
// Custom Middleware Function
const customMiddleware = ({ dispatch, getState }) => next => action => {
if (action.type !== 'HELLO_FROM_MIDDLEWARE') {
console.log('I am watching your actions', action);
dispatch({ type: 'HELLO_FROM_MIDDLEWARE' });
}
next(action);
};export default createStore(
combineReducers({ CommonReducer }),
applyMiddleware(sagaMiddleware, custom)
);Redux Saga (Effect Handling Modal)
Redux Thunk (Async operations in Redux)
Redux Logger (Logging the Redux Actions)
redux-saga is a library that aims to make application side effects (i.e. asynchronous things like data fetching and impure things like accessing the browser cache) easier to manage, more efficient to execute, easy to test, and better at handling failures
Source: https://redux-saga.js.org/
Generator Functions
Yield
Next
Yield*
Redux Saga uses Javascript's Generator functions to handle side effects
function* generator(k) {
yield k;
yield k + 5;
}
var gen = generator(25);
console.log(gen.next().value);
// expected output: 25
console.log(gen.next().value);
// expected output: 30
Call
import { put, takeLatest, all } from 'redux-saga/effects';
function* fetchNews() {
const json = yield fetch('https://newsapi.org/v1/articles?
source= cnn&apiKey=c39a26d9c12f48dba2a5c00e35684ecc')
.then(response => response.json(), );
yield put({ type: "NEWS_RECEIVED", json: json.articles, });
}
function* actionWatcher() {
yield takeLatest('GET_NEWS', fetchNews)
}
export default function* rootSaga() {
yield all([
actionWatcher(),
]);
}https://codesandbox.io/s/github/sarabs3UC/with-saga-meetup3