Documentation Index
Fetch the complete documentation index at: https://mintlify.com/Byrontosh/FundamentosReact/llms.txt
Use this file to discover all available pages before exploring further.
useFetch is a custom React hook that encapsulates reusable data-fetching logic. Instead of writing fetch boilerplate in every component, you call useFetch() once to receive fetchDataBackend — a ready-to-use async function. Pass any URL to that function and it will fetch the resource, parse the response as JSON, and return the result. Any network or parsing error is caught internally and logged to the console, keeping your component code clean.
Import
Signature
Parameters
useFetch itself takes no parameters. The async function it returns accepts the following argument:
The fully-qualified URL to fetch. The endpoint must respond with a valid JSON body; the hook calls
.json() on the raw Response object.Return value
useFetch returns fetchDataBackend, an async function with the signature (url: string) => Promise<any>.
When called, fetchDataBackend:
- Issues a
fetch(url)request using the browser’s native Fetch API. - Awaits the
Responseand calls.json()to deserialize the body. - Returns the parsed JavaScript value (object, array, primitive, etc.).
- On any error — network failure, non-JSON body, etc. — logs the error to the console via
console.logand implicitly returnsundefined.
Full source
Usage example
The following example is taken fromCuarto.jsx, the Custom Hook concept page. It demonstrates calling useFetch to obtain fetchDataBackend and using it inside two separate event handlers to load data from different APIs into local state.
Errors thrown during the fetch — including network failures, CORS rejections, and invalid JSON bodies — are caught by the
try/catch block inside fetchDataBackend and logged to the console with console.log(error). In these cases the function returns undefined. Your component should guard against an undefined result before accessing properties on the returned value.