use-http icon indicating copy to clipboard operation
use-http copied to clipboard

Typing of http methods

Open benesva4 opened this issue 4 years ago • 1 comments

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.

benesva4 avatar Jan 22 '21 15:01 benesva4

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')

iamthesiz avatar Jan 22 '21 18:01 iamthesiz