AI Hints
Home›React Tutorial›UI Patterns
React · Lesson 65 of 120

Practice: A Product Browser

Lists together.

⏱ 2 min read▶ Video at 53:43🎓 Free
Watch this lesson in the React courseWatch on YouTube ↗Full React course

Let's practise everything from this section. Load the products, search them, sort them, and show which ones are sold out.

  • Load
  • Search and sort
  • Sold out

A product browser

This combines the whole section. Products load from the API, once. The search and the sort order are state, and the shown list is derived. Sold out products are styled differently. We search for the letter i, and sort cheapest first. Run it.

App.jsxjsx
import { useState, useEffect } from "react";
import { getProducts } from "./api";
export default function App() {
  const [products, setProducts] = useState(null);
  const [query, setQuery] = useState("");
  const [cheapFirst, setCheapFirst] = useState(false);
  useEffect(() => { getProducts().then(setProducts); }, []);
  if (!products) return <p>Loading...</p>;
  const shown = products.filter(p => p.name.toLowerCase().includes(query.toLowerCase()));
  if (cheapFirst) shown.sort((a, b) => a.price - b.price);
  return (<div>
    <input id="q" value={query} onChange={e => setQuery(e.target.value)} placeholder="Search" />
    <label><input id="c" type="checkbox" checked={cheapFirst} onChange={e => setCheapFirst(e.target.checked)} /> Cheapest first</label>
    {shown.map(p => <div key={p.id} className={p.stock ? "box" : "box done"}>
      {p.name} — £{p.price} {p.stock === 0 && "(sold out)"}</div>)}
  </div>);
}
Also uses style.css
body{font-family:-apple-system,Helvetica,Arial,sans-serif;margin:22px;font-size:21px;color:#1d1d1f;background:#fafafa}
h1{font-size:34px;margin:0 0 14px} h2{font-size:26px;margin:0 0 10px}
button{font-size:19px;padding:9px 18px;border-radius:9px;border:0;background:#61dafb;color:#111;font-weight:600;margin:4px 6px 4px 0}
input{font-size:19px;padding:8px 10px;border:2px solid #bbb;border-radius:8px}
li{margin:5px 0} .box{padding:14px;border-radius:12px;background:#fff;border:2px solid #ddd;margin:8px 0}
.done{text-decoration:line-through;color:#999} small{color:crimson;display:block;font-size:16px} .big{font-size:64px;font-weight:800}
Also uses api.js
const PRODUCTS = [
  { id: 1, name: "Trail 500", price: 849.5, type: "bike", stock: 12 },
  { id: 2, name: "City 200", price: 450, type: "bike", stock: 0 },
  { id: 3, name: "Volt 7", price: 2199, type: "bike", stock: 4 },
  { id: 4, name: "Helmet", price: 40, type: "gear", stock: 30 },
  { id: 5, name: "Lights", price: 25, type: "gear", stock: 18 },
];
const wait = ms => new Promise(resolve => setTimeout(resolve, ms));

export async function getProducts() {
  await wait(300);
  return PRODUCTS;
}

export async function getProduct(id) {
  await wait(300);
  const product = PRODUCTS.find(p => p.id === Number(id));
  if (!product) throw new Error(`Product ${id} not found`);
  return product;
}
Also uses actions.json
[{"wait": 500}, {"type": "#q", "text": "i"}, {"click": "#c"}]

Summary

Load once, keep filters in state, and derive the rest. Next, the patterns quiz.

Key takeaways
  • Load once
  • State for filters
  • Derive the rest
Next up: Patterns Quiz — test yourself