Recipe Hub uses two custom hooks to encapsulate all async data fetching. This keeps pages and components free ofDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/Miguelcds/Recipe-Hub/llms.txt
Use this file to discover all available pages before exploring further.
useState/useEffect boilerplate and makes the fetching logic independently testable.
Both hooks reset their state (recipes, errors) before each fetch. This prevents stale data from a previous search appearing briefly while a new request is in flight.
useRecipes
Manages fetching multiple recipes — either by search query or as a random selection. Used by theHome page.
Source
src/hooks/useRecipes.js
Return values
| Value | Type | Description |
|---|---|---|
recipes | Array<{ id, name, picture, category }> | Normalized recipe objects from the last successful fetch |
loading | boolean | true while a fetch is in progress |
apiError | string | Error message from a failed fetch, or empty string |
fetchRecipes | (search: string) => Promise<void> | Fetches recipes matching the search query from TheMealDB |
fetchRandomRecipes | () => Promise<void> | Fetches 9 random recipes in parallel |
setApiError | (msg: string) => void | Allows the SearchBar component to set a validation error without triggering a fetch |
fetchRecipes vs fetchRandomRecipes
- fetchRecipes
- fetchRandomRecipes
Call
fetchRecipes(query) when the user submits a search term. It calls getRecipeByName and populates recipes with all matching results. If no results are found, the service throws and apiError is set.useRecipeDetail
Fetches and manages the state for a single recipe. Used by theRecipeDetail page, which passes the id URL parameter directly to this hook.
Source
src/hooks/useRecipeDetail.js
Return values
| Value | Type | Description |
|---|---|---|
recipe | object | null | Full recipe detail object, or null before the first fetch completes |
loading | boolean | true while the fetch is in progress |
apiError | string | Error message if the fetch fails, otherwise empty string |
Automatic re-fetch on id change
TheuseEffect dependency array includes id. If the user navigates directly from one recipe detail page to another (changing the :id URL param), the hook automatically re-runs the fetch for the new ID without requiring a full page reload.
The guard if (!id) return at the top of the effect prevents a fetch from firing when id is undefined — for example, during the initial render before the router has resolved the param.