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 / best practice pattern for calling async behavior when already in a reducer?

See original GitHub issue

Sorry 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 between reducers and effects seem to be that reducers are not supplied with send.

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

github_iconTop GitHub Comments

2reactions
timwiscommented, Aug 9, 2016

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):

effects: {
  fetch: (data, state, send, done) => {
    send('setLoading', () => {
      http('/url', (err, response, body) => {
        send('receive', body, done)
      }
    }
  }
},
reducers: {
  setLoading: (data, state) => ({ isLoading: true }),
  receive: (data, state) => ({ items: data, isLoading: false })
}

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!

0reactions
yoshuawuytscommented, Mar 21, 2017

Closing because choo@5 will introduce a new API. See #425 for the merged PR. Thanks!

Read more comments on GitHub >

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

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