I am using AWS-Amplify and when a user signs out I get a 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.
It mentions it occurs in the Profile.tsx file and this is the useEffect() hook.
But the issue is that the error occurs sometimes.
I keep testing it out and it comes and goes and I have no idea why it is so.
function Profile() {
const [user, setUser] = useState<IIntialState | null>(null);
useEffect(() => {
checkUser();
Hub.listen("auth", data => {
const { payload } = data;
if (payload.event === "signOut") {
setUser(null);
}
});
}, []);
async function checkUser() {
try {
const data = await Auth.currentUserPoolUser();
const userInfo = { username: data.username, ...data.attributes };
console.log(userInfo);
setUser(userInfo);
} catch (err) {
console.log("error: ", err);
}
}
function signOut() {
Auth.signOut().catch(err => console.log("error signing out: ", err));
}
if (user) {
return (
<Container>
<h1>Profile</h1>
<h2>Username: {user.username}</h2>
<h3>Email: {user.email}</h3>
<h4>Phone: {user.phone_number}</h4>
<Button onClick={signOut}>Sign Out</Button>
</Container>
);
}
return <Form setUser={setUser} />;
}