react_persist_login
react_persist_login copied to clipboard
Add check for canceled request in Axios useEffect
When the useEffect()
runs in dev environments, it actually runs twice. This can cause the abort controller to interrupt the operation and gets caught by the catch
on line 22 causing the user to be routed to the login page. Adding an if
statement and checking the err.name
attribute prevents the catch from calling navigate
. I also added axiosPrivate
, isLoading
, navigate
, and location
to the dependency array since they are defined outside the useEffect and it was causing the following warning:
React Hook useEffect has missing dependencies: 'axiosPrivate', 'location', 'navigate' and 'location'. Either include them or remove the dependency array react-hooks/exhaustive-deps.
Now with these changes, I can set isLoading to true and force a re-render and fetch of the data, such as when closing out a modal or other user driven action that might want to cause a refresh of the data.