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.

[Question] How do I dispatch multiple actions in one action?

See original GitHub issue

Hi.

Say I have one action initialize, for this as for many actions I am dispatching an action:start, action:success and action:failure. Most of these have counterparts in the reducers where they update properties like isFetching, isSaving, etc.

If I use redux-actions instead of redux-thunk then how do I let my reducers know the correct state at the correct time? If I can only dispatch one event at the end of one action then I can’t dispatch multiple actions/events from one action …

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

30reactions
addnabcommented, Jan 31, 2016

redux-thunk is a middleware unlike redux-actions which is a utility library for creating and handling actions. So you can still continue to use redux-thunk to dispatch multiple actions and use redux-actions API to createAction and handleActions like:

action.js

const fetchRequest = createAction('FETCH_REQUEST')
const fetchSuccess = createAction('FETCH_SUCCESS')
const fetchFailure = createAction('FETCH_FAILURE)
const fetch = (url) => {
    return async (dispatch, getState) => {
        try {
            dispatch(fetchRequest())
            const response= await _fetch(url)
            if(result.statusCode !== 200)
                throw new Error()
            const result = await response.json()
            dispatch(fetchSuccess(result))
        }
        catch(error) {
            dispatch(fetchFailure(error))
        }
    }
}

reducers.js

const reducer = handleActions({
  FETCH_REQUEST: (state, action) => ({
    ...state,
    fetchError: false,
    isFetching: true,
    displayText: "Fetching..."
  },

  FETCH_SUCCESS: (state, action) => ({
    ...state,
    fetchError: false,
    isFetching: false,
    displayText: action.payload
  }),

  FETCH_FAILURE: (state, action) => ({
    ...state,
    fetchError: true,
    isFetching: false,
    displayText: action.payload 
  })
});
0reactions
moersingcommented, Feb 28, 2018

@addnab Thank you , This is good solution.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Where to dispatch multiple actions in redux? - Stack Overflow
So, to answer the question, the right place to dispatch multiple actions is inside the click handler where the first action originates. Share....
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
Is there always a one-to-one mapping between reducers and actions? ... Should I dispatch multiple actions in a row from one action creator?...
Read more >
React Redux: performance considerations when dispatching ...
By default, React will re-render once for each dispatched action, including for actions that are dispatched together in the same tick of the ......
Read more >
Chaining Asynchronous Dispatches in Redux Toolkit - YouTube
In this video I cover how we can correctly dispatch one action after another, where the second dispatch is informed by the state...
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