Skip to content

Instantly share code, notes, and snippets.

@web-crab
Created February 21, 2026 22:58
Show Gist options
  • Select an option

  • Save web-crab/3e445bafa7cce89ad4507fee42efbbb9 to your computer and use it in GitHub Desktop.

Select an option

Save web-crab/3e445bafa7cce89ad4507fee42efbbb9 to your computer and use it in GitHub Desktop.
import { useEffect, useRef, useState } from 'react';
export function useAbort(init?: (controller: AbortController) => void) {
const [aborter] = useState(() => new AbortController());
const initRef = useRef();
initRef.current = init;
useEffect(() => {
initRef.current?.(aborter);
return () => aborter.abort();
}, [aborter]);
return aborter;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment