Custom hooks are JavaScript functions whose names start withDocumentation 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.
use and that can call other React hooks internally. They let you extract stateful or side-effectful logic out of individual components and share that logic across your entire application without changing the component tree. Instead of duplicating a fetch call — with its own loading state, error handling, and useEffect — in every component that needs remote data, you write the logic once inside a custom hook and import it wherever it is needed.
The useFetch hook
TheuseFetch hook lives in src/customHook/useFecth.js. It defines an inner async function called fetchDataBackend, which accepts a URL, calls the native fetch API, parses the JSON response, and returns the parsed data. The hook itself simply returns that function so that any component can store it and invoke it on demand.
fetchDataBackendis declared inside the hook, which keeps the async logic scoped and prevents accidental reuse of stale closures.fetch(url)returns aPromise<Response>. Calling.json()on the resolved response parses the body into a plain JavaScript object.- The
try/catchblock catches both network errors and JSON-parse failures, logging them to the console so they never silently swallow an exception. - Because
useFetchdoes not calluseStateoruseEffectinternally, it is a lightweight, stateless wrapper — state management is left to the consuming component.
Using useFetch in a component
Cuarto.jsx demonstrates the typical consumption pattern: call useFetch() at the top level of the component to obtain fetchDataBackend, then invoke that function inside async event handlers.
const fetchDataBackend = useFetch()— the hook is called at the top level ofCuarto, which is the only place hooks may be called (see the rules below). The returned async function is stored infetchDataBackend.getDataProductsandgetDataMemes— each handler is anasyncarrow function thatawaitsfetchDataBackendwith a different URL. BecauseuseFetchis stateless, the samefetchDataBackendreference works for every endpoint.setProducts/setMemes— once the data resolves, localuseStatesetters update component state and trigger a re-render to display the results.- Button
onClickbindings — clicking either button fires the corresponding handler; no data is fetched until the user acts, which avoids unnecessary network requests on mount.
Rules of hooks
React enforces two rules that apply to both built-in hooks and custom hooks:- Only call hooks at the top level. Never call hooks inside loops, conditions, or nested functions. React relies on the order in which hooks are called to associate internal state correctly across renders.
- Only call hooks from React functions. Hooks must be called from a function component or from another custom hook — never from a regular JavaScript utility function or class component.
