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.

Use transactional setState() inside connect()

See original GitHub issue

We currently delay calling mapStateToProps and mapDispatchToProps in the common case because it is too early to supply ownProps inside handleChange, as those props may themselves be obtained from the previous version of the state by the parent component, and may not have been received as the new props just yet, so we bump into issues with staleness (#86).

Right now we call them inside render() so ownProps are always up-to-date but this is a bit odd, makes the code complicated and in some cases robs us of possible performance improvements, as caching the element isn’t quite as good as providing shouldComponentUpdate (#366).

One possible solution to this would be to use the transactional setState() overlord overload. It is described in the documentation:

It’s also possible to pass a function with the signature function(state, props). This can be useful in some cases when you want to enqueue an atomic update that consults the previous value of state+props before setting any values. For instance, suppose we wanted to increment a value in state:

setState(function(previousState, currentProps) {
  return {myInteger: previousState.myInteger + 1};
});

This looks pretty much exactly like our use case, and I have a feeling that this would let us use setState normally without having to resort to calling mapStateToProps and friends in render(). In this case, we would probably keep the merged props rather than the store state, in the local state.

We still want to keep the “fast path” that avoids setState altogether in handleChange, but other than that, the rest of the code should be vastly simplified by this, or at least become more conventional.

I’m still not 100% sure this is going to work, but I encourage anyone who wants to dig deeper into how React and Redux work together to give this a try. We have an extensive test suite that should cover any call count regressions. If you are working on this, please leave a comment in this issue.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:7
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
amccloudcommented, Apr 25, 2016

Looking into it! I’m trying to wrap my head around what would no longer be necessary. Looks like this could clean up quite a bit of hand holding around haveStatePropsBeenPrecalculated and stateProps in render().

1reaction
slorbercommented, Apr 25, 2016

Really happy to see all those red lines on first commits 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why this.setState() is not working in the constructor?
The constructor is executed before the component is actually mounted and will not render something. That's why it makes no sense to use...
Read more >
Programmatic Transaction Management in Spring - Baeldung
The transactional aspect creates a new EntityManager and starts a new transaction, so it borrows one Connection from the connection pool. After ...
Read more >
React: Stop checking if your component is mounted - Medium
This article will provide solutions to the “Can't perform a React state update on an unmounted component” warning and why a call to...
Read more >
ALTER DATABASE SET Options (Transact-SQL) - SQL Server
Learn how to set database options such as Automatic tuning, encryption, Query Store in SQL Server, and Azure SQL Database.
Read more >
Reactive Transactions with Spring
Back in 2016, our reactive journey started with Spring Framework 5 ... instances use a single Context -bound transactional connection.
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