use-http
use-http copied to clipboard
Typing of http methods
Hello, is there a way to type individual methods that useFetch() returns? To me it seems logical to use the hook like this:
const { get, del, response, loading } = useFetch<ResponseType>("/user/"+ id)
const getUser= () => {
const userData= await get();
}
const deleteUser= () => {
const user = await del();
}
It feels kind of cumbersome to do something like:
const { get, response: responseGet, loading: loadingGet } = useFetch<GetResponseType>("/user/"+ id)
const { del, response: responseDel, loading: loadingDel } = useFetch<DelResponseType>("/user/"+ id)
....
Have I missunderstood the concept, or is it a technological limitation, or is there? Thank you very much.
Why don't you just do it the first way you suggested?
type ResponseType = GetResponseType | DelResponseType
const { get, del, response, loading } = useFetch<ResponseType>("/user/"+ id)
There's also the option below, but this hook was designed to do via 1 useFetch per base route
var [getRequest, getResponse, loading] = useFetch<GetResponseType>('https://example.com')
var [delRequest, delResponse, loading] = useFetch<DelResponseType>('https://example.com')