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.

render is called during or after dispatch depending on how dispatch was called

See original GitHub issue

Depending on how dispatch was called, render method is called after dispatch or during dispatch, increasing total dispatch execution time. I’ve created a tiny example project to explain it (look at console logs for operations order): https://codesandbox.io/s/XoLlRLMxl In 1st case (plain) we call dispatch directly and have the following order:

action
reducer
dispatch: 0.345947265625ms
render

In 2nd case (promise) we call dispatch from inside promise resolve callback and constantly getting a different order:

action
reducer
render
dispatch: 1.508056640625ms

3rd case (setTimeout) is leading to the same order as 2nd

Probably 2nd and 3rd case are ok and render stuff + dispatch itself taking the same time in total as in 1st case. But when we need to dispatch two or more actions after resolving promise we will get a huge overhead (that’s how I discovered this issue). So is it a bug or supposed to be? And if that’s ok then could it be controlled somehow?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
gaearoncommented, May 26, 2017

setState does not change behavior in 16, but will likely do in 17.

0reactions
timdorrcommented, Sep 23, 2017

If you need to run after rendering has finished, use the React lifecycle. componentDidUpdate is exactly what you’re looking for.

Ultimately, this isn’t something for Redux to solve. It’s just how React behaves. It’s just a matter of looking in the wrong place. You shouldn’t look to Redux or connect() to ensure rendering guarantees. That’s React’s job and it’s got lifecycle methods for it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Does render called or updated after dispatch and set state
It seems to work well. However my understanding is render should be called after dispatch. The member list in JSX is not updated....
Read more >
Different Ways to Dispatch Actions with Redux - Pluralsight
The dispatch method is available on the store object. An action gets dispatched to trigger an update to the Redux store. ... 1//...
Read more >
Hooks - React Redux
When the function component renders, the provided selector function will be called and its result will be returned from the useSelector() hook.
Read more >
Hooks FAQ - React
Hooks are called in the same order on every render. There are a few more heuristics, and they might change over time as...
Read more >
React useReducer Hook ultimate guide - LogRocket Blog
We dispatch action objects to that reducer only, whereas in Redux, ... that the useState updater function is newly called on each render....
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