Good Code
The good version makes the component boundary obvious: the component receives the display values it needs and renders them.
Lesson 01
Keep presentational components focused by passing the data they need explicitly.
type ReviewSummaryProps = {
title: string;
reviewerName: string;
status: "approved" | "changes-requested";
};
export function ReviewSummary({
title,
reviewerName,
status,
}: ReviewSummaryProps) {
// Explicit props make this component easy to move and test.
return (
<section>
<h2>{title}</h2>
<p>{reviewerName} marked this review as {status}.</p>
</section>
);
}import { useAppState } from "./app-state";
export function ReviewSummary() {
// Hidden app-state reads make the component depend on too much.
const { currentProject, activeReview, viewer } = useAppState();
return (
<section>
<h2>{currentProject.name}: {activeReview.title}</h2>
<p>{viewer.name} marked this review as {activeReview.status}.</p>
</section>
);
}The good version makes the component boundary obvious: the component receives the display values it needs and renders them.
The bad version reaches into broad application state, which couples a reusable summary to unrelated project, review, and viewer concerns.