I am making a fetch request to my server with the hook useEffect, and I keep getting this warning:
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.
I understand the reason for this warning, but how do I clean it up? I have tried methods suggested in this article with no luck: https://dev.to/pallymore/clean-up-async-requests-in-useeffect-hooks-90h
Here is my code:
import React, { useEffect, useState } from 'react';
import { Redirect, Route } from 'react-router-dom';
export default function ProtectedRoute({ component: Component, ...rest }) {
const [ user, setUser ] = useState('');
useEffect(() => {
const fetchUser = async () => {
try {
const response = await fetch('http://localhost:5000/user', {
credentials: 'include'
});
const data = await response.json();
setUser(data.session.passport.user);
} catch (error) {
console.log(error);
}
}
fetchUser();
return () => {
// Do some cleanup
}
}, [])
return (
<Route { ...rest } render={ () => user ? <Component /> : <Redirect to="/login" /> }/>
)
}
Any help is appreciated :)
UPDATE: Thanks to Tony Nguyen's solution below, here is my working code:
import React, { useEffect, useState } from 'react';
import { Redirect, Route } from 'react-router-dom';
import Loading from './Loading';
import Error from './Error';
export default function ProtectedRoute({ component: Component, ...rest }) {
const [ user, setUser ] = useState('');
const [ fetchingUser, setFetchingUser ] = useState(true);
const [ noError, setNoError ] = useState(true);
useEffect(() => {
const fetchUser = async () => {
try {
const response = await fetch('http://localhost:5000/user', {
credentials: 'include'
});
if (fetchingUser) {
const data = await response.json();
setUser(data.session.passport.user);
}
setFetchingUser(false);
} catch {
setNoError(false);
}
}
fetchUser();
}, [])
return (
<div>
<Route { ...rest } render={ () => { if (!noError) return <Error />} }/>
<Route { ...rest } render={ () => { if (fetchingUser && noError) return <Loading />} }/>
<Route { ...rest } render={ () => { if (user && !fetchingUser && noError) return <Component />} }/>
<Route { ...rest } render={ () => { if (!user && !fetchingUser && noError) return <Redirect to="/login" />} }/>
</div>
)
}
Your first piece of code is ok just need some modification that is adding fetchingUser
to let the component know you fetching user, do not redirect
import React, { useEffect, useState } from 'react';
import { Redirect, Route } from 'react-router-dom';
export default function ProtectedRoute({ component: Component, ...rest }) {
// add fetchingUser state with defaut is true
const [ fetchingUser, setFetchingUser ] = useState(true);
const [ user, setUser ] = useState('');
useEffect(() => {
const fetchUser = async () => {
try {
const response = await fetch('http://localhost:5000/user', {
credentials: 'include'
});
const data = await response.json();
setUser(data.session.passport.user);
// setFetching is false here
setFetchingUser(false)
} catch (error) {
// You can create an error state and set error here
console.log(error);
}
}
fetchUser();
return () => {
// Do some cleanup
}
}, [])
// add fetchingUser into conditions
// You can do better job that is if fetching show Loading component
// If error show error component
// if not fetching not error and have user show user component
// if not fetching not error and not user redirect to login
return (
<Route { ...rest } render={ () => user && !fetchingUser ? <Component /> : <Redirect to="/login" /> }/>
)
}