Menu
Coddy logo textTech

Step 9: Live Search

Part of the Next.js Essentials section of Coddy's React journey — lesson 47 of 47.

challenge icon

Challenge

Medium

Finish Coddy Eats with a live search.

  1. Create app/api/dishes/route.js exporting async function GET() that awaits getDishes() and returns Response.json(dishes).
  2. Create components/SearchBox.jsx with 'use client';, holding a query string and a matches array in state.
  3. Render <input id="search-input" type="text"> bound to query. On change: with empty text, clear the matches; otherwise fetch('/api/dishes'), await response.json(), and keep the dishes whose name contains the text (case-insensitively).
  4. Render <ul id="search-results" className="reviews"> with an <li> per match containing a <Link> with id={'match-' + dish.slug}, href={'/menu/' + dish.slug}, and the emoji, a space, then the name.
  5. Render <SearchBox /> inside the hero on app/page.jsx.

Try it yourself

import Link from 'next/link';
import './globals.css';

export const metadata = {
    title: {
        default: 'Coddy Eats',
        template: '%s | Coddy Eats',
    },
};

export default function RootLayout({ children }) {
    return (
        <html lang="en">
            <body>
                <nav className="nav">
                    <Link className="brand" href="/">Coddy Eats</Link>
                    <Link id="nav-menu" href="/">Menu</Link>
                    <Link id="nav-desserts" href="/?category=dessert">Desserts</Link>
                </nav>
                <div className="container">
                    {children}
                </div>
            </body>
        </html>
    );
}

All lessons in Next.js Essentials