-
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
question: idiomatic way to reference dispatcher and/or state? #20
Comments
From this comment in July, using redux-thunk and redux-promise together looks like a pretty good pattern:
|
thanks @kevana, i can get with that. it seems like it might be an improvement to have those characteristics in a single piece of middleware, but for now it could qualify as "idiomatic" ;) i'll leave this open as a placeholder for the idea of converging those middleware, but project owners can feel free to close if they feel this is a sufficient solution. |
@kevana, i just tried the strategy outlined above, and the behavior i witnessed was the thunk not being executed. the following doesn't take i think because
i have however had some luck with something like:
|
I don't have a working example, just stumbled across that comment and figured it might be helpful |
I do this
This way i can call the function with just the argument from the components, but I can also specify them explicitly in my tests. I don't use redux-actions. I just use plain redux connect() to bind and connect my action creators. |
Now that i am trying to render things server-side it looks like my way of importing the store directly is bad because on the serverside i must be able to change it at runtime from one request to another. What's the correct approach here ? |
The best way of dealing with this is to use an alternate thunk middleware that looks for the function in |
Lately i ended up removing the promised middleware and using just redux-thunk alone. Looks like a thunk can return a promised value and if it does the dispatch() return value itself is a promise that you can await. const actionCreator (params) => {
return async (dispatch, getState) => { ... }
}
await dispatch(actionCreator({...})) |
@davibe then you have an async reducer? I published redux-thunktions middleware that lets you handle this situation elegantly, there are some examples in the readme. |
I don't have an async reducer. Just the code i have written above works fine with redux-thunk. Inside that async method i can dispatch other async action but at some point the final actions actually modifying the state will be sync. |
I see, but then you'll need boilerplate each time to handle promises rejections etc. It's better to use promise middleware to handle that so you have a consistent and terse way of handling asynchronous payloads. |
I am not sure what you mean. I have a "client action creator" that can dispatch success/error depending on the outcome of the api call. I use it in many other actions so I am no really repeating that code. |
hi andrew,
redux-thunk facilitates referencing dispatcher and state by automatically providing the
dispatcher
andgetState
methods.how would you recommend gaining similar references using redux-promise?
regards,
tony
The text was updated successfully, but these errors were encountered: