Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement web request hook #127

Open
JRJurman opened this issue Oct 5, 2019 · 1 comment
Open

implement web request hook #127

JRJurman opened this issue Oct 5, 2019 · 1 comment

Comments

@JRJurman
Copy link
Member

JRJurman commented Oct 5, 2019

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

const [isLoading, isOkay, parsedResponse, rawResponse, promiseError] = useFetch(resource, requestOptions, responseBodyType, triggers)

In the above example, useFetch has the same parameters as fetch, a responseBodyType and triggers.

  • 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
@JRJurman
Copy link
Member Author

This is similar to React Query, but that depends on you calling fetch...

I'm reality, it might make more sense to have a usePromise hook, that allows for anything that takes time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant