Setup and verification
Node.js 22.23 or newer with npm. Python 3 is also required for the common verification entry point. Extract the learning package into a new folder and run python3 run_checks.py there. The script copies the included files into a temporary learning directory. It verifies the example and then the supplied solution there, preserving the original files. Required packages are downloaded on the first run.
Verified on 2026-10-20 on Linux x86_64. The test counts calls to the affected component and still checks visible results. A count is a regression signal, not a timing measurement. Use the React Profiler for actual interaction duration.
Memoization is useful only for repeated work with stable dependencies. This example separates filter input and visual theme. A test counts calls to the filter function. Changing theme must not recompute a cached filter. This demonstrates avoided work rather than a general improvement in perceived speed.
Package contents and individual files
01Understand the state model
memo skips a render only when props remain equal. New function or object values can defeat that comparison. First measure which part recalculates after an unrelated state change.
import { useMemo, useState } from "react";
const vehicles = Array.from({ length: 2000 }, (_, i) => `Vehicle ${i}`);
export const filter = (query: string) =>
vehicles.filter((name) => name.includes(query));
export default function App({
calculate = filter,
}: {
calculate?: (query: string) => string[];
}) {
const [query, setQuery] = useState("");
const [dark, setDark] = useState(false);
const rows = useMemo(() => calculate(query), [calculate, query]);
return (
<main
style={{
background: dark ? "#18332f" : "white",
color: dark ? "white" : "#18332f",
}}
>
<h1>Vehicle search</h1>
<label htmlFor="query">Search</label>
<input
id="query"
value={query}
onChange={(e) => setQuery(e.target.value)}
/>
<button onClick={() => setDark((x) => !x)}>Toggle theme</button>
<p role="status">{rows.length} matches</p>
<ul>
{rows.slice(0, 20).map((row) => (
<li key={row}>{row}</li>
))}
</ul>
</main>
);
}
02Start the project
Install the pinned dependencies and run npm run dev. Data consists of synthetic learning values. Open the local address and trigger the action yourself.
{
"name": "appfabrik-learning",
"private": true,
"type": "module",
"scripts": {
"dev": "vite --host 127.0.0.1",
"build": "tsc --noEmit && vite build",
"test": "vitest run src"
},
"dependencies": {
"react": "19.2.3",
"react-dom": "19.2.3",
"react-router-dom": "7.18.4",
"@tanstack/react-query": "5.104.5"
},
"devDependencies": {
"vite": "8.1.5",
"typescript": "6.0.3",
"@types/react": "19.2.14",
"@types/react-dom": "19.2.3",
"@types/node": "22.19.15",
"vitest": "4.1.10",
"@testing-library/react": "16.3.2",
"@testing-library/user-event": "14.6.1",
"jsdom": "29.1.1",
"@playwright/test": "1.64.1"
}
}
03Verify the contract with a test
The test counts calls to the affected component and still checks visible results. A count is a regression signal, not a timing measurement. Use the React Profiler for actual interaction duration.
// @vitest-environment jsdom
import { test, expect, vi, afterEach } from "vitest";
import { render, screen, cleanup } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import App, { filter } from "./App";
afterEach(cleanup);
test("theme changes preserve cached work and visible result", async () => {
const calculate = vi.fn(filter);
render(<App calculate={calculate} />);
const before = calculate.mock.calls.length;
await userEvent.click(screen.getByRole("button", { name: "Toggle theme" }));
expect(calculate).toHaveBeenCalledTimes(before);
expect(screen.getByRole("status").textContent).toBe("2000 matches");
await userEvent.type(screen.getByLabelText("Search"), "1999");
expect(screen.getByRole("status").textContent).toBe("1 matches");
expect(screen.getByRole("listitem").textContent).toBe("Vehicle 1999");
});
Run / check
npm install
npm test
npm run buildThe test counts calls to the affected component and still checks visible results. A count is a regression signal, not a timing measurement. Use the React Profiler for actual interaction duration.
04Observe failures and boundaries
Change the expected output and observe a failed test. A TypeScript build protects type contracts while a browser test protects interactions. The two checks serve different purposes.
05Implement the exercise
Verify clearing the query restores all results.
06Verify the solution
The solution checks both directions of the state change. useMemo must not affect the domain result. Temporarily remove it and compare call counts. For timing use a production build and document input, machine and multiple runs. Copy the solution files to the corresponding locations in the learning project and run tests and build again.
Tests and type checking pass. The observed result matches the learning goal.
Your exercise
Verify clearing the query restores all results.
Show the solution and explanation
The solution checks both directions of the state change. useMemo must not affect the domain result. Temporarily remove it and compare call counts. For timing use a production build and document input, machine and multiple runs.
// @vitest-environment jsdom
import { test, expect, vi, afterEach } from "vitest";
import { render, screen, cleanup } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import App, { filter } from "./App";
afterEach(cleanup);
test("theme changes preserve cached work and visible result", async () => {
const calculate = vi.fn(filter);
render(<App calculate={calculate} />);
const before = calculate.mock.calls.length;
await userEvent.click(screen.getByRole("button", { name: "Toggle theme" }));
expect(calculate).toHaveBeenCalledTimes(before);
expect(screen.getByRole("status").textContent).toBe("2000 matches");
await userEvent.type(screen.getByLabelText("Search"), "1999");
expect(screen.getByRole("status").textContent).toBe("1 matches");
expect(screen.getByRole("listitem").textContent).toBe("Vehicle 1999");
await userEvent.clear(screen.getByLabelText("Search"));
expect(screen.getByRole("status").textContent).toBe("2000 matches");
});
Project context and sources
This independent learning project explores a topic in this technology area. Project references provide a pinned revision as architectural context. They do not imply that every library or feature introduced here is already used in that application.