Last active
June 7, 2017 02:04
-
-
Save acr13/7cb0a6c6846431815ad845b4d364f86f to your computer and use it in GitHub Desktop.
Simple Saga API Request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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