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.

mapStateToProps is not always called synchronously

See original GitHub issue

found a very hard to trace bug in my app and it seems like mapStateToProps is not being called the same. I’m able to replicate the behaviour in codesandbox

https://codesandbox.io/s/m075w1wq8

AppContainer is the parent AppContainer2 is the child

before dispatch
parent mapStateToProps 1
after dispatch
child mapStateToProps 1

check the console.log and found that parent’s mapStateToProps is being called synchronously, which is within the dispatch call, but the child’s mapStateToProps is being called after dispatch call

I think this is because the subscription in the connect function is trying to defer it after componentDidUpdate in the parent

please close this if this is intended

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
markeriksoncommented, Jun 28, 2021

The dispatches themselves are synchronous. The re-rendering of the UI is batched.

This issue really isn’t a good place to provide support. If you’re having problems, the best option is to ask in either Stack Overflow or Reactiflux, and provide a CodeSandbox or repo that demonstrates whatever problem you’re seeing, rather than sort of poking around at implementation details.

1reaction
markeriksoncommented, Jun 28, 2021

See my other post A (Mostly) Complete Guide to React Rendering Behavior for details on how React batching works.

The other factor is that React-Redux v7 also uses batching internally.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Redux state update doesn't map to props immediately ...
I'm referring to react-redux, where the state updates should be synchronous, afaik. I know they could be batched, but not sure if that...
Read more >
Connect: Extracting Data with mapStateToProps - React Redux
Much like a Redux reducer, a mapStateToProps function should always be 100% pure and synchronous. It should only take state (and ownProps )...
Read more >
Defining and Using React Redux mapStateToProps Function
mapStateToProps function should be pure and synchronous. ... This means no asynchronous HTTP calls, setTimeout , async functions, etc.
Read more >
Extracting Data with mapStateToProps - LWC Redux
Much like a Redux reducer, a mapStateToProps function should always be 100% pure and synchronous. It should only take state (and ownProps) as...
Read more >
Recipes - Zustand Documentation - Pmndrs.docs
Just call set when you're ready, zustand doesn't care if your actions are async or not. const useStore = create((set) => ({ fishies:...
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