How to / best practice pattern for calling async behavior when already in a reducer?
See original GitHub issueSorry to log this as an issue, but I cannot find an explanation in the guide.
In a reducer:
function reducer(){
if (foo !== null) {
state.message = 'foo loaded'
} else {
doSomethingAsync(state, send)
state.message = 'loading foo'
}
return state
}
This is where doSomethingAsync would otherwise be under effects
and then call another reducer to update the message when foo
is loaded, but I would need to
- call it directly from this point (eg
model.effects.doSomethingAsync
) and - somehow pass it
send
so that it could notify when finished (not an option since the main difference betweenreducers
andeffects
seem to be thatreducers
are not supplied withsend
.
I could move this logic into the view and then if foo is not loaded, send an action to call a reducer
specifically to indicate loading -and- another to call an async function, but this just moves the logic into and doubles the length of the code in my view versus a single action when a button is clicked…
Would love a little guidance on this, thanks!
Issue Analytics
- State:
- Created 7 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Redux Fundamentals, Part 6: Async Logic and Data Fetching
It only knows how to synchronously dispatch actions, update the state by calling the root reducer function, and notify the UI that something ......
Read more >Async actions in bare Redux with Thunk or custom middleware
Learn how to manage asynchronous actions in React apps with Redux Toolkit, or a bare Redux implementation with custom middleware.
Read more >How to Make Asynchronous Calls in Redux Without ... - Velotio
Now as we have our store defined, let us see how we will manipulate the statewith different phases that an API call can...
Read more >React best practices and patterns to reduce code
1. Create custom hooks for redux actions and dispatches · 2. Use object instead of switch inside the reducer · 3. Create a...
Read more >The State Reducer Pattern with React Hooks - Kent C. Dodds
Downshift is currently implemented as a render prop component, because at the time, render props was the best way to make a "Headless...
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 FreeTop 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
Top GitHub Comments
Hello! If I’m understanding you correctly, you want to show a loading indicator, execute asynchronous logic, and turn off the loading indicator when it’s finished. This is a common use case and I agree we should have more examples of it. I could probably find some examples in the wild but I’m on my phone so I may have to come back to do that.
What you want to do is use an effect, and have the effect call reducers using send. The final argument you give send is a callback that’s executed when whatever action you’re sending is completed. Usually you just pass this the done callback but you can also write your own callback function. For instance (pardon typos):
Might make sense to use a separate send to turn off the reducer, at which point you enter callback hell, and something like run-series from the async library can help. Hope this helps!
Closing because
choo@5
will introduce a new API. See #425 for the merged PR. Thanks!