How to call an action in a different slice from a nested asynchronous action
See original GitHub issueI have a (nested) login() action which is inside an auth
object/namespace and an updateLoading
function that is not inside that namespace (it is called from other places in my app). Something like this:
const reducers = module.exports = {
updateLoading: loading => state => ({
loading
}),
auth: {
login: () => (state, actions) => {
actions.updateLoading(true)
}
// Do the login etc ...
},
// ...
}
Now, since login is asynchronous, I want it to call the updateLoading()
while doing the http post request to the login API but the actions
object that it receives contains only the actions that are inside the auth
object/namespace.
Is it possible to call the upadeLoading
action from inside auth.login
?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:3
- Comments:11 (7 by maintainers)
Top Results From Across the Web
Redux ToolKit: is it possible to dispatch other actions from the ...
I have two kinds of async operations: get the data from the API server. update the data on the API server. export const ......
Read more >Async Dispatch Chaining with Redux-Thunk | Jscrambler Blog
One potential solution is to lump all async code into a single thunk: // Illustration only, AVOID this const combinedThunk = () =>...
Read more >Gatsby Slice API
The createSlice action from the createPages API is the first step in creating a new Slice. gatsby-node.js. Copygatsby-node.js: copy code to clipboard. exports ......
Read more >Redux Fundamentals, Part 7: Standard Redux Patterns
The official Fundamentals tutorial for Redux: learn the standard patterns used in real-world Redux apps.
Read more >Recipes - Zustand Documentation - Pmndrs.docs
Recipes · Fetching everything · Selecting multiple state slices · Fetching from multiple stores · Memoizing selectors · Overwriting state · Async actions...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@JorgeBucaran no problem - as others have pointed out in this thread there are various ways this issue can be solved.
However, since most users trying to implement non-toy apps will probably stumble upon this problem, I recommend adding some mention of this problem and possible solutions with pros and cons to the docs (or maybe add another doc like best practices where problems like this would be discussed).
Yes I understand your argument about breaking up the state/actions to different modules but I don’t think that having an optional
global_actions
argument would do any harm - if people understand the consequences (their actions object have a specific structure) then why not use it and make their lives easier?I also tried your fourth solution and it works really nice - although I agree that it feels a little “hackish” and is not very easy to understand.