expo/use-unmount-signal

Question: Can it be used with axios?

Closed this issue · 1 comments

If yes, please provide example of using this hook with axios.

Different implementation for axios. It uses cancel tokens.

import { useEffect, useState } from 'react';
import axios from 'axios';

function useUnmountSignal() {
  const [source] = useState(() => axios.CancelToken.source());

  useEffect(() => {
    return () => {
      source.cancel();
    };
  }, []);

  return source.token;
}

function Example() {
    const cancelToken = useUnmountSignal();
    return (
      <button
        onClick={() =>
          axios.get('https://ping.example.com', { cancelToken })
        }>
        Ping
      </button>
    );
}