qol-hooks
Version:
A collection of React hooks to improve the quality of life of developers.
26 lines (25 loc) • 772 B
TypeScript
type FetchResult = [any | null, boolean, Error | null];
/**
* @description A hook to fetch data from an API
* @param {string | (() => string)} url The URL to fetch data from
* @param {RequestInit} options The fetch options
* @returns {FetchResult} The data, loading state, and error
*
* @example```tsx
* const [data, loading, error] = useFetch("https://api.example.com/data", {
* method: "GET",
* headers: {
* "Content-Type": "application/json",
* },
* });
* if (loading) {
* return <p>Loading...</p>;
* }
* if (error) {
* return <p>Error: {error.message}</p>;
* }
* return <pre>{JSON.stringify(data, null, 2)}</pre>;
* ```
*/
declare function useFetch(url: string | (() => string), options: RequestInit): FetchResult;
export default useFetch;