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.

Wasteful operations when mapStateToProps has 2 arguments, store triggers change event but props and state unchanged

See original GitHub issue

As title suggests, when mapStateToProps is using both state and ownProps to select chunk of redux state, when store has some state updated, but the particular chunk of state which is returned by mapStateToProps has not been changed, connect produces wasteful render attempt. This is specifically bad in places when there is thousands of components wrapped with connect, or there is just couple of such components, store is updated quite frequently, but state of wrapped components is not changed. Issue is not reproduced when mapStateToProps is not using ownProps.

Performance degradation is seen in the task manager and in JS profiler, and becomes a visually noticeable problem on documents with complex DOM in Edge and Firefox. Measurements from React.addons.perf.printWasted show that on 10 seconds intervals the wasteful operations can take more than 1 second, the wasteful operations are happening inside Connect(Component).

Looking into profiling using Chrome profiler, a lot of time is spent inside ReactComponent.setState, looking into source code of connect.js, the problem is apparently in the handleChange.js, it does an unconditional this.setState when this.doStatePropsDependOnOwnProps is true. Perhaps an option to customize this behavior, e.g. by introducing support for custom comparer would be really nice.

Small sample project with reproduction has been set in https://github.com/vlan-saxo/react-redux-bug, use npm install, npm start and go to http://localhost:8080/webpack-dev-server/

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
jimbollacommented, Jun 21, 2016

I think this will be addressed by #407. I’m rewriting the Connect component to only fire setState() if the final props have changed. I’m seeing HUGE performance gains.

0reactions
PCreationscommented, Jul 9, 2016

Exactly, and I don’t know why either…

Read more comments on GitHub >

github_iconTop Results From Across the Web

reactjs - What is the purpose of mapStateToProps when State ...
mapStateToProps function tells react-redux what data a particular component needs from the redux store. This function gets the whole state ...
Read more >
REACT Flashcards - Quizlet
Study with Quizlet and memorize flashcards containing terms like How do we change ... A component cannot change its props, but it can...
Read more >
Redux, Thunk, Middleware, Context API, Action, Reducer ...
Only an event can change the state of the store. The function that returns the new state needs to be a pure function....
Read more >
You Might Not Need The mapDispatchToProps Function
First, a mapStateToProps function that plucks pieces of state out of Redux and assigns them to props that your React component will use....
Read more >
React Redux Integration: Part 1 of a detailed guide from ag-Grid
The reducer that is associated with the action type will then transform the state which is saved in the store. Reducers are pure...
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