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

useEffect memory leak when setting state in callback #21443

Closed
iamfazal opened this issue May 6, 2021 · 3 comments
Closed

useEffect memory leak when setting state in callback #21443

iamfazal opened this issue May 6, 2021 · 3 comments
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug

Comments

@iamfazal
Copy link

iamfazal commented May 6, 2021

I know the Issue #15006 is still open but the described problem is not fit in my case.
I have to check the User is Login? using AWS Cognito function in the private route. I can't use the cleanup function because before the callback executes I redirect to my login page.

here is some of the code
Privateroute.js (problem in this code)

import { Route, Redirect } from "react-router-dom";
import React, { useState, useContext, useEffect } from "react";
import { AccountContext } from "../components/cognito";

export default function PrivateRoute({ component: Component, ...rest }) {
  const [status, setStatus] = useState(false);
  const { getUserStatus } = useContext(AccountContext);
  useEffect(() => {
    let mounted = true;
    getUserStatus().then((data) => {
        if(mounted)
            setStatus(true);
        
    });
    return () => mounted = false;
  },[]);

  return (
    <Route
      {...rest}
      render={(props) =>
        status ? <Component {...props} /> : <Redirect to="/" />
      }
    />
  );
}

if I removed the mounted check I received the memory leak warning

code to check the is user login

const getUserStatus= async()=>{
    await new Promise((resolve,reject)=>{
      const user = UserPool.getCurrentUser();
      if(user){
        user.getSession((err,data)=>{
          if(err)
            reject(false)
          else{
            resolve(data)
          }
        })
      }else
        reject(false)
    })
  }

call to private route

<Router>
    <Account className="App">
      <Switch>
        <Route exact path="/" component={Login} />
        <Route path="/sign-in" component={Login} />
        <Route path="/sign-up" component={Signup} />
        <Route path="/forgotPassword" component={ForgotPassword} />
        <PrivateRoute path="/dashboard" component={Dashboard} />
      </Switch>
    </Account>
  </Router>
@iamfazal iamfazal added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label May 6, 2021
@eps1lon
Copy link
Collaborator

eps1lon commented May 6, 2021

if I removed the mounted check I received the memory leak warning

Sounds like there is no issue with the posted code then? Could you provide a repro that does have an issue?

@iamfazal
Copy link
Author

iamfazal commented May 7, 2021

@eps1lon there is a logical error. my user is login and before the promise resolved the status hook is false so I redirect to "/" page.

@PupoSDC
Copy link

PupoSDC commented May 9, 2021

The posted code looks correct to me as well. With promises, you always need to do some sort of check to make sure its still safe to update the state on your component.

I can't use the cleanup function because before the callback executes I redirect to my login page.

This shouldn't be true. When you go to "/" your component will still call the PrivateRoute useEffect cleanup. You can test this by adding a breakpoint, to that particular line (or a debugger/console.log call).

@iamfazal iamfazal closed this as completed Oct 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug
Projects
None yet
Development

No branches or pull requests

3 participants