Skip to content

Instantly share code, notes, and snippets.

@edrpls
Created July 25, 2019 18:59
Show Gist options
  • Save edrpls/ce3b96f8703f22d6a30cd53770e55eb9 to your computer and use it in GitHub Desktop.
Save edrpls/ce3b96f8703f22d6a30cd53770e55eb9 to your computer and use it in GitHub Desktop.
// First import our http helper to the top of the file, you can use whatever you want, maybe just a simple fetch call
import * as http from './helpers/http';
// ...
// action that will pass the clients from the server request to the reducer
// will be 'dispatched' after the async request is successful
function saveClientsToStore(clients) {
return {
type: 'GET_CLIENTS',
clients
};
}
// Async action that will make a server request to get the list of clients
export function getClients() {
// redux-thunk not only helps redux perform async actions, but it also makes the
// redux dispatch available for any action this in turn let's us return another
// action instead of an action creator object
return dispatch =>
http
.get('/clients')
// Call the dispatcher to pass the received data to the reducer
.then(clients => dispatch(saveClientsToStore(saveClientsToStore)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment