0

I am new to React and I am getting this error. Can anyone help me on how to sort this. Thanks!

Full Warning Statement: 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.

Sample code:

    useEffect(() => { 
    fetchBlogs();
    const subscription = DataStore.observe(Blog).subscribe(() => fetchBlogs());
    return ()  =>{
        subscription.unsubscribe();
    };

}, [])


async function fetchBlogs() {
    const blogData = await DataStore.query(Blog);


    const sortedBlogs = blogData.sort((a,b) =>  b.createdAt > a.createdAt);


     if (findTrueItem(sortedBlogs) !== undefined)
    {  
         arrayMove(sortedBlogs,findPosition(sortedBlogs), 0);
     }
    
    setBlogs(sortedBlogs);
} 
Jem Bro
  • 1
  • 3
  • I think this may be helpful [Can't perform a React state update on an unmounted component](https://stackoverflow.com/questions/53949393/cant-perform-a-react-state-update-on-an-unmounted-component) – Dina Matveev Jul 26 '21 at 21:36
  • @DinaMatveev Thanks for your help! I have checked the link you have provided and I have added a conditional statement to unmount some of the statements in my code. So far the Warning issue does not show up but we will keep on observing if the same issue occurs. Thanks! – Jem Bro Jul 27 '21 at 12:29

0 Answers0