You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For most web-app projects, making a web request is almost always required. It would be really valuable and convenient if tram-one included a hook that was the common implementation of useEffect + fetch.
responseBodyType is a value that is by default JSON, and changes parsedResponse. It can be any function name that returns a promise for Response.Body, and if it fails, will populate the promiseError value.
triggers is a list of values, and is the same as the triggers passed into useEffect (these will effectively be passed down to the useEffect, and can make a fetch call repeat multiple times)
Instead of returning a promise, it returns:
isLoading - true if the request is being made, false if the request has resolved or been rejected
isOkay - undefined if the request until it is resolved, and then either true or false based on the fetch response.ok
prasedResponse - based on the responseBodyType passed in, will be the result of either json(), text(), formData(), arrayBuffer() or blob()
rawResponse - the original response object
promiseError - if the promise for fetching or the promise for parsing the data fails, this variable contains the error
The text was updated successfully, but these errors were encountered:
Summary
For most web-app projects, making a web request is almost always required. It would be really valuable and convenient if tram-one included a hook that was the common implementation of useEffect + fetch.
Potential Interface
In the above example,
useFetch
has the same parameters asfetch
, aresponseBodyType
andtriggers
.responseBodyType
is a value that is by defaultJSON
, and changesparsedResponse
. It can be any function name that returns a promise forResponse.Body
, and if it fails, will populate thepromiseError
value.triggers
is a list of values, and is the same as thetriggers
passed intouseEffect
(these will effectively be passed down to the useEffect, and can make a fetch call repeat multiple times)Instead of returning a promise, it returns:
isLoading
- true if the request is being made, false if the request has resolved or been rejectedisOkay
- undefined if the request until it is resolved, and then eithertrue
orfalse
based on the fetchresponse.ok
prasedResponse
- based on theresponseBodyType
passed in, will be the result of eitherjson()
,text()
,formData()
,arrayBuffer()
orblob()
rawResponse
- the original response objectpromiseError
- if the promise for fetching or the promise for parsing the data fails, this variable contains the errorThe text was updated successfully, but these errors were encountered: