The best place for network calls is in your action creators. However, you will need some middleware to make this work better. Take a look at the promise-middleware (in fact, I would suggest checking out the entire tutorial). If you use this middleware, you can have action creators that return a promise, and also have three types of actions: one for requesting, one for processing successful responses, and one for processing failed requests. Then you just listen to these 3 actions in your gearboxes.
So, with this middleware, you can create an action creator as follows:
function networkCall() { return { types: ['MAKE_REQUEST', 'REQUEST_SUCCESS', 'REQUEST_FAILURE'], promise: () => { return new Promise((resolve, reject) => { $.ajax({ url: 'example.com/api' type: 'GET' }); }) } } }
Obviously, you are free to create your own middleware, but that should put you in the right direction.
Andy noelker
source share