-
-
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
mapStateToProps called on componentWillUnmount #1360
Comments
I'm kind of confused on the sequencing you're trying to describe. Can you provide a CodeSandbox that demonstrates the issue, preferably with some logging that annotates the expected vs actual sequence of events? |
@markerikson Well my guess is that with react-router (and the withRouter HoC), a soon as we navigate to a different route, See what I mean? I'm not sure if that's a react-router issue, redux issue, or if it is even an issue at all :) But this problem is constantly happening for us. I can try to provide a CodeSandbox, but I'm not sure I can reproduce it easily... |
It's mainly an "issue" with React Router. I use quotes because it's really an expected behavior. React doesn't give us fine grained control over when other components in the tree mount or unmount. Nor would we really want that, because it sounds like a nightmare to control. Instead, I would make the suggestion that relying on one component's unmount to "clean up" Redux state is probably going to be problematic in the long run. Tracking that is simple right now, but could easily become a huge problem if your app gets any more complex. More ideally, your components should be able to load up whatever they need independently of each other and Redux can serve as the central point for them to converge. The most obvious thing you should do is get rid of the generic |
@timdorr We did what you suggested at first. But as you know, we constantly need to find the right balance between productivity/maintainability. In this specific app, most of the screens are very similar(ish). We made that choice for productivity concerns and for being able to deliver according to the fixed roadmap. It may not seem like a great productivity increment but in the long run it is. As we are able to create new screens really fast. While this is not really an issue, it's yet kind of a pain in the ass if you ask me ;-) Having said that, thanks for your feedbacks! We'll try to find another approach without sacrifying too much productivity. |
Ho and about "relying on one component's unmount to clean up Redux state" not being scalable. It doesn't seem to be so problematic to me since we have a common list "container" (smart) being the entry point of every single list throughout the app. Using the At some point we need to do clean ups and I can't see another way than using the React components lifecycle for that. How you'd do it? |
In general, |
@markerikson In this case the problem is not that |
@benjamindulau : uh... now I'm really confused.
Please review the action log using the Redux DevTools Extension to see what action is being dispatched and figure things out from there. I highly suggest turning on the "action stack trace" feature to see the exact code path that is dispatching that action. As I said earlier, a demo that shows the actual vs expected behavior would also help me see what's going on here, because the explanations aren't very clear to me atm. |
redux v4.04
react-redux v7.1.0 (same issue with 6.x)
Maybe related to #351 or #1226.
In a smart component, I have an unexpected call to
mapStateToProps
when executingcomponentWillUnmount()
To provide you with some context:
We're in a CRM with some routes rendering similarish lists of items like users, leads, messages, etc.. We use the same smart component for controlling the List behavior. A list can be rendered by specifying a
type
as prop so the container can tell the action which entity types it should fetch and the selector which type it should return as results. Everything is normalized using normalizr in a sharedentities
substate.Here is a (shortened) code sample where I can experience this issue:
The
getResults
selector is similar to:the
App
component above would be somewhat like:So the issue occurs when:
leads
type results. So at this pointstate.list.result
would contains something like["lead-1", "lead-2", "lead-3"]
. The switch/case does the job and the list renders correctly./messages
which also renders a List, but with items of typemessages
.You can see that we try to properly reinitialize the result by dispatching the
actions.list.reinitialize()
action. This would basically updates the state like the following in the reducer:But we get an error, because the
mapStateToProps
executes and at this pointstate.list.result
still contains:["lead-1", "lead-2", "lead-3"]
and not[]
so the List container would try to select items of typemessages
which are not yet loaded.Shouldn't the state be up to date when
mapStateToProps
is called? The expected behavior would be thatcomponentWillUnmount
executes, the state updates to an empty result list and , only then,List
container is rendered again with a call tomapStateToProps
.The text was updated successfully, but these errors were encountered: