Skip to content

Instantly share code, notes, and snippets.

@jtomaszewski
Last active July 9, 2019 06:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtomaszewski/1a13fb5027bce5108aa033b300d04ebd to your computer and use it in GitHub Desktop.
Save jtomaszewski/1a13fb5027bce5108aa033b300d04ebd to your computer and use it in GitHub Desktop.
import { call, put, takeLatest } from "redux-saga/effects";
// Action creator function
function fetchArticles(params) {
return { type: FETCH_ARTICLES, params };
}
// Sagas
function* fetchArticlesSaga() {
yield takeLatest(FETCH_ARTICLES, triggerFetchArticles);
}
function* triggerFetchArticles({ params }) {
yield put({ type: FETCH_ARTICLES_START });
try {
const response = yield call(
ArticlesService.getArticles,
params
);
yield put({
type: FETCH_ARTICLES_SUCCESS,
data: response.data
});
} catch (error) {
yield put({ type: FETCH_ARTICLES_FAILURE, message: error });
}
}
// Configuration
const sagaMiddleware = createSagaMiddleware();
const store = createStore(
reducer,
applyMiddleware(sagaMiddleware)
);
sagaMiddleware.run(fetchArticlesSaga);
// Actual usage
dispatch(fetchArticles(params));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment