reduxjs / react-redux

Official React bindings for Redux
https://react-redux.js.org
MIT License
23.36k stars 3.37k forks source link

mapStateToProps should not be called with functional component rendering a connected class component #2024

Open pandaiolo opened 1 year ago

pandaiolo commented 1 year ago

What version of React, ReactDOM/React Native, Redux, and React Redux are you using?

What is the current behavior?

Description:

Reproduction:

Here is the logging, by app, when toggling off connected children (conditional rendering):

With a functional component as parent:

[APP1] CLASS CHILD MAP STATE TO PROPS
[APP1] NORMAL CHILD SELECTOR
x2 [APP1] NORMAL PARENT SELECTOR
[APP1] RENDER PARENT {showChild: false}
[APP1] NORMAL PARENT SELECTOR
[APP1] RENDER PARENT {showChild: false}

With a class component as parent:

[APP2] CLASS PARENT MAP STATE TO PROPS
x2 [APP2] RENDER PARENT {showChild: false}
[APP2] NORMAL CHILD SELECTOR

Note:

Maybe all of this is a normal consequence of the inherent difference in React rendering functional or class component, but I still wanted to document this in an issue for the record, and see if you have any comment.

Thanks!

What is the expected behavior?

Which browser and OS are affected by this issue?

Chrome OSX

Did this work in previous versions of React Redux?

markerikson commented 1 year ago

I'm not sure what you're trying to describe here, tbh.

At my first guess (just reading the description), I assume that the difference in mapState calls is not a bug, but simply a question of which components are re-rendering and which are not, per https://blog.isquaredsoftware.com/2020/05/blogged-answers-a-mostly-complete-guide-to-react-rendering-behavior/#standard-render-behavior .

pandaiolo commented 1 year ago

I have tried to explain as accurately as possible and provided a codesandbox, but if it is unclear or need more details, I'll be happy to provide them.

What I'm pointing is that the two examples in that demo are rendering exactly the same thing (supposedly) but mapState is called on one side and not on the other, which was unexpected and led to a bug (on our side), so I'm reporting on that.

markerikson commented 1 year ago

@pandaiolo yeah, afraid I don't have time to look at this right now (busy with day job stuff).

But, my immediate guess is that:

which sounds like a combination of how React itself works in general for rendering rather than a particular bug in React-Redux.

I'll try to look into this at some point, but not sure when.

pandaiolo commented 1 year ago

Sure, thank you!