Part 16 / Topic 2 of 9
Colocation
examples/02_colocation/ is one component, not two parallel versions, because this is what feature folders look like once you stop stopping at one file per folder: FavoriteButton.jsx imports useFavorite.js (its own hook, not a shared top-level hooks/useFavorite.js) and FavoriteButton.module.css (its own scoped styles, not a shared global.css), and FavoriteButton.test.jsx sits right beside all three.
// examples/02_colocation/FavoriteButton.jsx
import { useFavorite } from "./useFavorite.js";
import styles from "./FavoriteButton.module.css";
export function FavoriteButton({ restaurantName }) {
const [isFavorite, toggle] = useFavorite();
return (
<button onClick={toggle} className={isFavorite ? styles.active : ""}>
{isFavorite ? "♥" : "♡"} {restaurantName}
</button>
);
}
Clicking the button really flips "♡ Paradise Biryani" to "♥ Paradise Biryani" and really applies a real CSS-Module class name - scoped to this file alone, not the literal global string "active" - which FavoriteButton.test.jsx checks by asserting the rendered class name is non-empty rather than a hardcoded string, since a CSS Module's actual generated name isn't meant to be guessed at from outside the file. This is the same habit this course's own examples/*.test.jsx files have modeled since Part 1: a test lives beside the file it tests, not in a separate top-level tests/ folder - colocation was never a new idea introduced here, this section just names it and applies it to a hook and a stylesheet too.
Done here? The practice set for Part 16 — including the solutions — is on the part page.