-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
connect(mapStateToProps) is executed with component props based on the previous state #86
Comments
Why do you say the previous state? I think they are called with the most recent state—and because the I don't think this is something we can fix. React state changes are asynchronous and React may (or may not) batch them. Therefore, the moment you press “Remove”, the Redux store updates, and both Unless I'm mistaken, there is nothing we can do. You have two options:
Potentially we could have the component generated by |
Because the store has been updated and the props
Hmm. Is it actually required for the both to be subscribed to the store? Wouldn't it be enough if only the one most closest to the component root is subscribed? When that component renders it will trigger a render (and a store query) in all child components. Seems like it would solve this. Although I have no idea how to detect the one closest to the root. |
Darn. Just realized it would break apps using pure components. Because the props between the parent and child components does not necessary change it might prevent some children from updating because React thinks there's nothing to the update but there might be because the store has changed. |
I find it really counter intuitive that you have consider state transitions in the store query. This really feels a bug to me but I see how it's unresolvable with the current React APIs. When thinking about the roots of this issue I think it can be nailed down to the fact that
This seems to be the right way to do it. Although I sometimes feel that I have to introduce new props to intermediate components just for them to passing those down. Which feels ugly. But I'm not sure it really is because it solves so many issues and weird edge cases like this. Could Redux just prevent people from ending up with this situation? Maybe by removing |
We removed it before, and people were unhappy. We do warn in the documentation that we encourage you to follow React flow and avoid |
Heh, before today I would have been one those unhappy people but after pulling my hair out while debugging this today my opinion is certainly different. Sadly I missed that bit of the docs (or didn't take it seriously at the time ). |
To whom who are looking for a workaround for this: An interesting observation is that if you do the selection in the component render method the issue is not present at all. Example: https://github.com/epeli/redux-connect-demo/pull/1/files The down side is that your component will do more useless re-renders as you'll have to pass in data that is not actually used for rendering. |
Superseded by #99. |
Ok, this is really hard to explain so I've set up an example here:
https://github.com/epeli/redux-connect-demo
Build it and play with it with devtools console opened. You should see following errors:
This happens because the
mapStateToProps
of theItem
component is called with component props based on the previous store state. The two states are not compatible anymore and it causes all kinds of errors.I think the parent should render and update the props for the
Item
component before themapStateToProps
of theItem
component can be executed.The text was updated successfully, but these errors were encountered: