Skip to content
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

Cancel InteractionManager's task on unmount #233

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

avet-m
Copy link

@avet-m avet-m commented Jun 8, 2021

There are some cases when interaction manager's task executing when component have already unmounted and React return a warning when setState happed on unmounted component. The whole warning you can see below:

Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function

So this tiny fix will cancel task on component unmount.

Fix for this warning:

 Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function
Comment on lines +8 to +13
const {cancel} = InteractionManager.runAfterInteractions(() => {
updateInteractionStatus(true)
})

return () => {
cancel?.()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

runAfterInteractions returns a promise and I'm not sure if they are required to have their methods bound so that they work without this being set 🤔

Also, I think that cancel will always be set, right? It's documented as returning a cancelable promise 🤔

What do you think about this instead:

Suggested change
const {cancel} = InteractionManager.runAfterInteractions(() => {
updateInteractionStatus(true)
})
return () => {
cancel?.()
const promise = InteractionManager.runAfterInteractions(() => {
updateInteractionStatus(true)
})
return () => {
promise.cancel()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants