-
Notifications
You must be signed in to change notification settings - Fork 279
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
CRS-292 Prevent state updates on unmounted Channel component #566
Conversation
… state update if component has already been unmounted.
Size Change: +264 B (0%) Total Size: 1.25 MB
ℹ️ View Unchanged
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
src/components/Channel/Channel.js
Outdated
@@ -138,6 +138,7 @@ const ChannelInner = ({ | |||
const lastRead = useRef(new Date()); | |||
const chatContext = useContext(ChatContext); | |||
const online = useRef(true); | |||
const isMounted = useRef(true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have seen this weird issue with our previous class based components as well. LGTM
@ath92 Maybe can we extract it to separate hook?
const isMounted = useIsMounted();
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose this hook would basically be a combination of useRef
for storing the flag + useEffect
for setting it to true/false?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just did this, also allowed me to write a test quite easily :)
Would be nice if we could also get rid of the root cause of the warning. In this case, cancel the promise or abort the request. |
It seems But I don't think that would actually get rid of state updates; I think in this case cancelling the request will add little benefit: it would require additional stateful logic (i.e. we'd have to keep the axios cancelToken somewhere, and make sure the request actually gets cancelled on unmount). If the server handles cancellation requests, I think in most cases this request would arrive at the server too late to make a difference. |
@ath92 think this will do for now, let's get this merged and released. |
Description of the pull request
This prevents React from logging a warning to the console if the
Channel
component finishes itsloadMore
callback after it has already been unmounted.Steps to reproduce:
loadMore
functionloadMore
is still fetchingNote: I spent quite some time trying to write a test case for this, but it seems that it's very hard to figure out if a component tries to update its state after it has been unmounted. For example, React prevents the actual reducer from being called, so you cannot just spy on that to know if there was an attempted state update.