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.

How to call an action in a different slice from a nested asynchronous action

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

4reactions
spapascommented, Feb 25, 2018

@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).

4reactions
spapascommented, Feb 16, 2018

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.

Read more comments on GitHub >

github_iconTop 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 >

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