Skip to content
engineering workbench.by StudyPort Academy
All projects
Advanced90 min

Recipe Box

Browse a local set of recipes, search them, open a detail route, and keep favorites in sync. Apply the React lessons in one small app.

ComponentsListsEffectsContextRouting

YOUR BUILD CHECKLIST

  1. Render RecipeCard, RecipeList, and SearchBar from the seed data, with a stable key on each card.
  2. the controlled search with useEffect and clear the timer in cleanup.
  3. Open /recipes/:id from a card, using React Router.
  4. Share favorites through useFavorites(), from both the list and the detail view.
  5. Memoize the filtered list and the toggle passed into each card.
  6. Keep the search test green: a query must narrow the visible list.

STARTER

The seed data and the component files are already split apart. The search test starts red. Build the six stages in the checklist. The preview should say Search test: pass when the filter narrows the list.

import { useState } from "react";
import { MemoryRouter, Route, Routes } from "react-router-dom";
import { recipes } from "./data/recipes";
import { RecipeList } from "./components/RecipeList";
import { SearchBar } from "./components/SearchBar";
import { FavoritesProvider } from "./favorites";
import { runSearchTest } from "./search.test";

export default function App() {
  const [query, setQuery] = useState("");
  const result = runSearchTest();
  return (
    <FavoritesProvider>
      <MemoryRouter initialEntries={["/recipes"]}>
        <p>Search test: {result}</p>
        <Routes>
          <Route
            path="/recipes"
            element={
              <>
                <SearchBar value={query} onChange={setQuery} />
                <RecipeList recipes={recipes} />
              </>
            }
          />
        </Routes>
      </MemoryRouter>
    </FavoritesProvider>
  );
}

Solution walkthrough

Each stage points at the lesson that already taught the idea. Open a stage when you are stuck, not before you try it.

1. Static render

Uses JSX and components, Rendering lists

Keep RecipeCard, RecipeList, and SearchBar in their own files. RecipeList maps the seed array and puts recipe.id on each li. The seed file is already complete. Do not fetch anything.

Open the lesson
2. Search and filter

Uses Events and state, Effects and side effects

SearchBar stays controlled. Store the typed query immediately, then copy it into a debounced query inside useEffect after 300ms. Return a cleanup that clears that timeout so a fast typist does not apply an older query. filterRecipes should match name or description, and return every recipe when the query is blank.

Open the lesson
3. Detail view and routing

Uses Routing

MemoryRouter keeps /recipes and /recipes/:id working inside this preview. Link each card to its id. RecipeDetail reads the id with useParams and renders that recipe. A deployed app would swap in a browser router; the paths stay the same.

Open the lesson
4. Favorites

Uses Sharing state

The list and the detail view must see the same saved ids. Put that set in FavoritesProvider and read it only through useFavorites(). Do not call useContext at each card.

Open the lesson
5. Performance pass

Uses Performance

useMemo belongs on the filtered list: that array is derived data, and it should change only when the debounced query changes. useCallback belongs on the toggle passed into RecipeCard, because that card is wrapped in memo. Leave SearchBar and the detail heading alone.

Open the lesson
6. One test

Uses Testing components

search.test.js calls filterRecipes with the query lentil. It passes only when the result is a shorter list that contains only lentil-soup. The preview line “Search test” reports pass or fail. Finish filterRecipes before you expect that line to turn green.

Open the lesson

Make it your own

The original works. These are your next experiments.

  • Optional: sort the visible list by prep time.
  • Optional: add a category filter beside search.
  • Optional: persist favorite ids so they survive a refresh.
Open a fresh playground