Skip to content

Instantly share code, notes, and snippets.

@antialias
Created August 16, 2018 19:59
Show Gist options
  • Save antialias/82c61b5766343cb3bdd72e9fb9acbd2a to your computer and use it in GitHub Desktop.
Save antialias/82c61b5766343cb3bdd72e9fb9acbd2a to your computer and use it in GitHub Desktop.
Simple Thunk API Request
import {
API_BUTTON_CLICK,
API_BUTTON_CLICK_SUCCESS,
API_BUTTON_CLICK_ERROR,
} from './actions/consts';
import { getDataFromAPI } from './api';
const getDataStarted = () => ({ type: API_BUTTON_CLICK });
const getDataSuccess = data => ({ type: API_BUTTON_CLICK_SUCCESS, payload: data })
const getDataError = message => ({ type: API_BUTTON_CLICK_ERROR, payload: message });
const getDataFromAPI = () => {
return dispatch => {
dispatch(getDataStarted());
getDataFromAPI()
.then(data => {
dispatch(getDataSuccess(data));
}).fail(err => {
dispatch(getDataError(err.message));
})
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment