React error: "error 'AbortController' is not defined "

Viewed 145

I built a mini-website with react - everything was working fine until I logged on today. Now I cannot render it and instead get the following error: "AbortController' is not defined".

Part of the code is:

const useFetch = (url) => {
    const [data, setData] = useState(null);
    const [isPending, setIsPending] = useState(true);
    const [error, setError] = useState(null);

    useEffect(() => {
        const abordCont = new AbortController();

        setTimeout(() => {
            fetch(url, { signal: abordCont.signal })
            .then(res => {
                if(!res.ok) {
                    throw Error('could not fetch data for that recource');
                }
                return res.json();
            })
            .then(data => {
                setIsPending(false);
                setData(data);
                setError(null);
            })
            .catch(err => {
                if (err.name === 'AbortError') {
                    console.log('fetch aboarded');
                } else {
                    setIsPending(false);
                    setError(err.message);
                }
            })
        }, 1000);

        return () => abordCont.abort();
    }, [url])

    return {error, isPending, data};
}

I already tried upgrading node, it is in the newest version already (v17.9.0) and going back to version 16, but nothing helped. Any ideas on how to solve this?

Thanks in advance!

0 Answers
Related