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

192
Views
Cleanup async tasks in useEffect using react/typescript using react-query/axios

Not sure I am doing this correct, I am getting this error:

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 am using React/typescript with React query and Axios. Code so far:

    const [fetchData, setFetchData] = React.useState<IData[]>([]);
    const [displayData, setDisplayData] = React.useState<IData[]>([]);

    const queryKey = 'planes';

    const getAlertsFunction = async (): Promise<any> => {
            const source = axios.CancelToken.source()
            await new Promise(resolve => setTimeout(resolve, 1000))
            const response = await axios.get('https://xxxxx', { cancelToken: source.token }).then(res => res.data);
            setFetchData(response);
            setDisplayData(response);
            console.log('Latest response is', response);
            response.cancel = () => {
                source.cancel('Query cancelled')
            }
            return response;
    }

    const queryInfo: any = useQuery(queryKey, getAlertsFunction,
        {
            refetchOnWindowFocus: true,
        }
    );

    useEffect(() => {
        console.log('Mounted');
        return () => {
        console.log('Un-mounted');
        axios.CancelToken.source()
        };
    }, [displayData]);

I am not entirely sure what needs to be passed into the useEffect hook to avoid this error and cleanup.

about 4 years ago · Juan Pablo Isaza
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!