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 called when component not due to render

See original GitHub issue

Hi, I have a conditionally displayed component, based on a value in redux. All components below are connected.

eg in a wrapper component’s render method: { (showValueInRedux) ? <ComponentUsingShowValue/> : <EmptyComponent/> }

ComponentUsingShowValue has a mapStateToProps that uses redux state that it expects to be there, hence we guard for that with showValueInRedux in the wrapper component.

In version 6, mapStateToProps in ComponentUsingShowValue is only called when that component is due to render.

In version 7, mapStateToProps is called even if the wrapping component has switched to choosing EmptyComponent due to showValueInRedux being false (after having once been true).

This causes an exception in our current code because the redux values that mapStateToProps uses ( in the now non-rendered component) are not available.

Please could you clarify this change in behaviour?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:24 (17 by maintainers)

github_iconTop GitHub Comments

1reaction
MrWolfZcommented, Apr 26, 2019

I have further minimized the sandbox and the problem seems clear now: in certain situations two dispatches just after another will lead to checkForUpdates (the store subscription callback) being called twice after another. If the result of the second call doesn’t change, it will immediately trigger notifyNestedSubs. This causes the issue. I will spend some time later today or tomorrow to turn this into a test case and then hopefully come up with a fix (though I don’t really have an idea right now how this could be fixed).

1reaction
MrWolfZcommented, Apr 24, 2019

I made the sandbox much smaller, so that it is easier to analyze. I need to go to sleep now, and I will only be able to continue on Friday, but maybe somebody else can solve this. Based on my reduced example the issue can only be due to either the timing of the async fetch action in the select inbox thunk or due to a state update occuring inside componentDidUpdate in InboxView.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Component not re-rendered after updating the state ...
mapStateToProps is called but the component is not re-rendering. This is my component class Users extends Component { render() ...
Read more >
Connect: Extracting Data with mapStateToProps - React Redux
The first argument to a mapStateToProps function is the entire Redux store state (the same value returned by a call to store.getState() )....
Read more >
Basic Tutorial | React Redux
If you call connect with only mapDispatchToProps , your component will: not re-render when the store changes; receive each of the action creators...
Read more >
React Redux connect(): When and how to use it
They are not aware of the Redux state. Presentational components get their data from props and may trigger callbacks passed to them via ......
Read more >
React Redux
Accidentally mutating or modifying your state directly is by far the most common reason why components do not re-render after an action has...
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