Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

109
Views
React -hook UseEffect memory Leak issue more than one function call

Hello Everyone I am new to react-hook I have some doubts about the useEffect call method

1 . I have one warning issue react Hook useEffect has a missing dependency "get data". Either include it or remove the dependency array react-hooks/exhaustive-deps I fixed that issue by using this command line // eslint-disable-next-line react-hooks/exhaustive-deps, if I am doing like this correct or not if any issue occurs in future production server.

  1. I have another issue in useEffect if I routing quickly I got this kind of error "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 tried to search fix the problem I saw the answer in a single API call in the within inside useEffect, I have a more than one get API call how can I write a cleanup function, Any Ideas or Links

Thanks for Help

const [user,setUser]=useState([]);
const [post,setPost]=useState([]);

const UserList =()=>{
  axios.get("https://jsonplaceholder.typicode.com/users")
.then((res)=>{
  setUser(res.data)
 })
}

const PostList =()=>{
  axios.get("https://jsonplaceholder.typicode.com/post")
.then((res)=>{
  setPost(res.data)
 })
}

useEffect (()=>{
PostList();
UserList();
},[])
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Your API calls are not asynchronous, so the state is not updated until the API call is complete, but the components are rendered onto the screen which depends on the values of the state. Make your API calls async.

about 4 years ago · Juan Pablo Isaza Report

0

Memory leaks happens when you try to set a state after your component is unmounted and this often happens when you set your state in async/promise functions.

One solution that I have been using for a while is initiating a variable outside component and check that variable before updating your state.

let isMounted = true;
const YourComponent = () => {
  const UserList = () => {
    axios.get("https://jsonplaceholder.typicode.com/users")
    .then((res)=>{
      if(isMounted){
        setUser(res.data)
      }
    })
  }

  useEffect(() => {
    UserList();
    return () => {
      isMounted = false;
    }
  }, []}
}
about 4 years ago · Juan Pablo Isaza Report

0

after fetch data, need cleanup function. UseEffect(callback, deps) allows you to easily cleanup side-effects. When the callback function returns a function, React will use that as a cleanup function

useEffect(() => {
let controller = new AbortController();    (async () => {
  try {
   //fetch
  } catch (e) { 
    // Handle fetch error
  }
})();
return () => controller?.abort();  }, []);

obviously this is one option how you can deal with it

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!