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

Prevent state changes in useAxios for unmounted components #67

Closed
wants to merge 2 commits into from

Conversation

chris-peerman
Copy link

This pull request fixes the following React error if an Axios request completes on an unmounted component. It does this by using a React ref object to keep track of whether the object has been dismounted or not. If the component has been dismounted it will not try to update its state which would trigger this error to occur.

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

Several of the tests have also been updated to avoid the following error being reported in the tests:

      Warning: An update to TestHook inside a test was not wrapped in act(...).
      
      When testing, code that causes React state updates should be wrapped into act(...):      
      act(() => {
        /* fire events that update state */
      });
      /* assert on the output */

Updated tests to remove React warnings about dismounted component updating themselves
useAxis will no longer update state when dismounted
@simoneb
Copy link
Owner

simoneb commented Nov 7, 2019

Thanks for your contribution, indeed request cancellation wasn't supported and you provided a good initial implementation. I'm closing this because I chose a different implementation option which is more comprehensive, because it allows cancelling pending requests when the component refetches in addition to when it unmounts, and it relies on axios's built-in cancellation mechanism. Your implementation had a race condition in that case, due to the use of a global flag. You can see what I did in 8cf1f74.

@simoneb simoneb closed this Nov 7, 2019
@chris-peerman
Copy link
Author

Thanks for looking at this. Unfortunately the cancel token code change doesn't fix the the issue we were trying to fix, and instead adds more things problems to the console error which we're experiencing. I've created an issue with more details how to reproduce the error we were trying to fix and including the test rig we were using here #74.

@simoneb
Copy link
Owner

simoneb commented Nov 9, 2019

Excellent @chris-peerman, thanks and sorry for closing this then, I thought this change would address the problem. I have a clue what might be the reason why you're still seeing errors, it may be something I missed in the change I did. I'll have a look at the issue you submitted.

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