Question: Can it be used with axios?
Closed this issue · 1 comments
Kailash23 commented
If yes, please provide example of using this hook with axios.
wobsoriano commented
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>
);
}