mapStateToProps is not always called synchronously
See original GitHub issuefound 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:
- Created 5 years ago
- Reactions:1
- Comments:12 (5 by maintainers)
Top 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 >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
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.
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.