Skip to content

Instantly share code, notes, and snippets.

@acr13
Last active June 7, 2017 02:04
Show Gist options
  • Save acr13/7cb0a6c6846431815ad845b4d364f86f to your computer and use it in GitHub Desktop.
Save acr13/7cb0a6c6846431815ad845b4d364f86f to your computer and use it in GitHub Desktop.
Simple Saga API Request
import { call, put, takeEvery } from 'redux-saga/effects';
import {
API_BUTTON_CLICK,
API_BUTTON_CLICK_SUCCESS,
API_BUTTON_CLICK_ERROR,
} from './actions/consts';
import { getDataFromAPI } from './api';
export function* apiSideEffect(action) {
try {
const data = yield call(getDataFromAPI);
yield put({ type: API_BUTTON_CLICK_SUCCESS, payload: data });
} catch (e) {
yield put({ type: API_BUTTON_CLICK_ERROR, payload: e.message });
}
}
// the 'watcher' - on every 'API_BUTTON_CLICK' action, run our side effect
export function* apiSaga() {
yield takeEvery(API_BUTTON_CLICK, apiSideEffect);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment