1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-12-02 05:57:29 +01:00
market/src/@hooks/useCancelToken.ts

21 lines
477 B
TypeScript
Raw Normal View History

import { useRef, useEffect, useCallback } from 'react'
import axios, { CancelToken } from 'axios'
export const useCancelToken = (): (() => CancelToken) => {
const axiosSource = useRef(null)
const newCancelToken = useCallback(() => {
axiosSource.current = axios.CancelToken.source()
return axiosSource?.current?.token
}, [])
useEffect(
() => () => {
if (axiosSource.current) axiosSource.current.cancel()
},
[]
)
return newCancelToken
}