question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dispatching multiple actions in an action creator

See original GitHub issue

What is the recommended way of doing something like this? Using redux-thunk, you receive dispatch in your thunk, but with this middleware, i’m not sure how i’d go about getting access to the dispatch method in my action creators. Sorry if i’m just missing something obvious.

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:2
  • Comments:13

github_iconTop GitHub Comments

2reactions
nicmesancommented, Sep 12, 2016

Guys, I managed to get it work by using the Promise.all() method. Check the example below:

deleteMultipleVideos(videoIdsArray){
    var responses = [];
    //iterate through every item and get it corresponding promise
    videoIdsArray.forEach((videoId)=>{
        var url = `${API_BASE_PATH}/assets/${videoId}`;
        var response = axios.delete(url, DEFAULT_REQUEST_CONFIG);
       //pushing all the promises into an array
        responses.push(response);
    });
    //waiting for all promises to be resolve before reaching the reducer
   //and then returning the array with the resolved promises
    return Promise.all(responses);
}

Hope it helps!

1reaction
antokaracommented, Jan 4, 2018

Try this one:

const getRemoteDataActionFn = () => dispatch => (
  dispatch(createAction(
    'GET_REMOTE_DATA',
    async () => fetchAndDispatchExtraActionFn(dispatch),
  )())
);

It uses redux-thunk , redux-actions and redux-promise I believe this is what you originally wanted @ashaffer

Read more comments on GitHub >

github_iconTop Results From Across the Web

Where to dispatch multiple actions in redux? - Stack Overflow
The action creator is the correct location for dispatching multiple actions. Although code like the following would work:
Read more >
Can I dispatch multiple actions from Redux action creators?
So it is OK to dispatch multiple actions from an action creator? Yes! There is absolutely nothing wrong with doing so. Don't worry,...
Read more >
Redux FAQ: Actions
How do you decide between thunks, sagas, observables, or something else? Should I dispatch multiple actions in a row from one action creator?...
Read more >
Javascript: Redux: Dispatching Multiple Actions - Medium
The answer of course is, yes, just either call dispatch multiple times or iterate dispatch over an array. Usually, though, if you're dispatching...
Read more >
Multiple Actions | React Redux Tutorial for Beginners in Hindi
Can I dispatch multiple actions from Redux action creators ?Follow Tutorials website:FB: https://www.facebook.com/tutorialswebsiteWebsite: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found