Skip to content

Instantly share code, notes, and snippets.

@kentcdodds
Created April 3, 2020 23:32
Show Gist options
  • Star 35 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save kentcdodds/b36572b6e9227207e6c71fd80e63f3b4 to your computer and use it in GitHub Desktop.
Save kentcdodds/b36572b6e9227207e6c71fd80e63f3b4 to your computer and use it in GitHub Desktop.
function useAbortController() {
const abortControllerRef = React.useRef()
const getAbortController = React.useCallback(() => {
if (!abortControllerRef.current) {
abortControllerRef.current = new AbortController()
}
return abortControllerRef.current
}, [])
React.useEffect(() => {
return () => getAbortController().abort()
}, [getAbortController])
const getSignal = React.useCallback(() => getAbortController().signal, [
getAbortController,
])
return getSignal
}
@Krisztiaan
Copy link

Thank you for engaging with me on this random piece of discussion.

The original code still creates an AbortController, even if it's never "used", for the useEffect cleanup.

Removing the callback, and the unintended instantiation, this seems slightly less readable but functionally more fitting to presumed intention, with no loose ends.

function useAbortController() {
  const abortControllerRef = React.useRef<AbortController>()

  React.useEffect(() => {
    return () => abortControllerRef.current?.abort()
  }, [])

  const getSignal = React.useCallback(() => {
    if (!abortControllerRef.current) {
      abortControllerRef.current = new AbortController()
    }
    return abortControllerRef.current.signal
  }, [])

  return getSignal
}

@kentcdodds
Copy link
Author

Solid 👍👍

@PerryRylance
Copy link

Hello folks, could someone kindly show me how this works in practise?

I'm currently doing this with a ref, this looks more elegant though

@nzkks
Copy link

nzkks commented Jul 21, 2023

Whoever found above gist first before Kent's own tweet, use the above hook like below. Directly copied from that tweet.

const getSignal = useAbortController()

// in an effect
fetch('/thing', {signal: getSignal()})

@webgodo
Copy link

webgodo commented Aug 3, 2023

I use it this way, but it raises error: Failed to execute 'fetch' on 'Window': The user aborted a request.

  const getSignal = useAbortController();
  const [data, setData] = useState();

  const doFetch = async () => {
    const signal = getSignal();
    const response = await fetch("https://codesandbox.io", { signal });
    const result = await response.body;

    setData(result);
  };

  useEffect(() => {
    doFetch()
  }, []);

How to solve?

@charlestbell
Copy link

How do you manually call abort() from this?

@charlestbell
Copy link

charlestbell commented Aug 8, 2023

Here is my manually controlled abortController I use to allow users to cancel an upload in React Native

function useAbortController() {
  const abortControllerRef = useRef();
  const getAbortController = useCallback(() => {
    console.log('getAbortController', abortControllerRef.current);
    if (!abortControllerRef.current) {
      abortControllerRef.current = new AbortController();
    }
    return abortControllerRef.current;
  }, []);

  const abortSignal = useCallback(() => {
    if (abortControllerRef.current) {
      abortControllerRef.current.abort();
 
      abortControllerRef.current = null;      // Resets it for next time
    }
  }, []);

  const getSignal = useCallback(
    () => getAbortController().signal,
    [getAbortController]
  );

  return { getSignal, abortSignal };
}

@sagarpanchal
Copy link

Typescript useAbortController.ts

import React from "react"

export function useAbortController() {
  const abortControllerRef = React.useRef<AbortController | undefined>()

  const getAbortController = React.useCallback(() => {
    abortControllerRef.current =
      abortControllerRef.current && !abortControllerRef.current.signal.aborted
        ? abortControllerRef.current
        : new AbortController()
    return abortControllerRef.current
  }, [])

  const getSignal = React.useCallback(() => {
    return getAbortController().signal
  }, [getAbortController])

  React.useEffect(() => {
    return () => {
      getAbortController().abort("Re-render")
    }
  }, [getAbortController])

  return getSignal
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment