-
Notifications
You must be signed in to change notification settings - Fork 119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dispatching multiple actions in an action creator #14
Comments
Also wondering about this. I need to make an API call to load data that depends on the result of another API call. What's the correct way to do this? |
Could we have something like
for addressing dependencies between actions? So when the action is done will fire two actions of types type1 and type2 with the payload. in the middleware:
|
@gabrielgiussi I created a package to address this problem in a general way: redux-multi. Just return an array of actions from an action creator and it'll dispatch them all. EDIT: I realize now this doesn't actually fully address the issue if you are using redux-promise. I am also working on an effect-middleware system that does address this issue over in redux-effects, though, if you'd like to check it out. |
That's great, but with redux-multi you can't achieve what @tlrobinson is asking for, don't you? I've been wondering if an action that triggers another action fit in in the flux/redux pattern, so we start with a ADD_TODO that makes an async api call to the server, when it backs we could trigger ADD_TODO_SUCCESS or ADD_TODO with 'done'. Can we fire a new action of another type (for example SEND_EMAIL for notify the added todo) when an action of type ADD_TODO is fired? |
@acdlite Anyway to do this kind of sequential triggering of events in |
redux-effects let's you do exactly this. |
@ashaffer Could you give an example of using |
@gsklee redux-effects would be used in place of redux-promise. An example using redux-effects-etch and redux-multi might look like this: import {fetch} from 'redux-effects-fetch'
import {bind} from 'redux-effects'
import {createAction} from 'redux-actions'
const userIsLoading = createAction('USER_IS_LOADING')
const userDidLoad = createAction('USER_DID_LOAD')
const userDidError = createAction('USER_DID_ERROR')
function getCurrentUser () {
return [
bind(fetch('/user'), userDidLoad, userDidError),
userIsLoading()
]
} redux-effects dispatches the return value of all of your handlers. So when function getUsersComments (userId) {
return bind(
fetch('/user/' + userId),
user => bind(fetch(user.commentsUrl), commentsDidLoad)
)
} |
👍 for multiple dispatches per action. |
Guys, I managed to get it work by using the Promise.all() method. Check the example below:
Hope it helps! |
I ended up writing https://github.com/Industrial/redux-streams since I'm not that into promises :-) |
@nicmesan Belated thanks man. Your solution works. Just let your action return a Promise composed of an array of Promises with Promise.all(arrayOfPromises) and redux-promise will take care of solving all of them. |
Try this one:
It uses redux-thunk , redux-actions and redux-promise |
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.The text was updated successfully, but these errors were encountered: