The Analogy: A Letterpress Shop, Its Formes, and the Job Ticket
An old letterpress print shop does not draw each poster by hand. The compositor arranges metal type once and locks it into a steel frame called a chase; the locked block is a forme. Bolted onto the press, that forme strikes the same impression onto sheet after sheet -- five hundred identical handbills, each one exactly like the last. The value of the forme is precisely that it is fixed: build the arrangement carefully one time, then reuse it as many times as the run demands without rethinking a single letter.
What varies from run to run does not live in the forme. It lives on a job ticket clipped to the order: which ink color, which paper stock, how many sheets, and the one line of text to overprint for this customer. The pressman reads the ticket and carries out exactly what it says. The ticket is his input, handed down from whoever took the order, and it is not his to rewrite. His craft is to execute the order faithfully, not to decide it.
Because the forme is fixed and the ticket is the only thing that changes, one forme serves countless jobs: the same wedding-invitation layout prints with a different couple's names on each ticket. Larger pages are built by composition -- several small formes, a headline block, a body block, a footer rule, locked side by side into one chase and run together as a single page. Small, well-made formes combine into big results without anyone re-cutting type.
The discipline breaks the instant someone treats the ticket as scratch paper. If the pressman pencils a "correction" onto the order, the next station downstream reads his altered ticket instead of the customer's real request, and the job drifts from what was agreed. Worse, if two separate jobs are somehow clipped to the same physical ticket card, a change meant for one run silently rewrites the other. Every failure here comes from the same sin: mutating an input that was only ever meant to be read.
Syntax or Core Structure
A component is a function. Its single argument is the props object -- the job ticket. It returns a description of what to render. In real React that description is JSX (a forme of elements); the browser then strikes it to the DOM.
function Welcome({ name, role }) {
return (
<li>
{name} -- {role}
</li>
);
}
That JSX return has no plain-text output to trace on its own -- it is an element tree the renderer strikes later. To see the logic deterministically, model the same component as a pure function of its props that returns a string, and reuse it across a data array exactly as the press reuses one forme across many tickets.
function Welcome({ name, role }) {
return name + " -- " + role;
}
function Team({ people }) {
return people.map((p) => Welcome(p)).join("\n");
}
console.log(Team({ people: [
{ name: "Sara", role: "Design" },
{ name: "Cahal", role: "Backend" },
] }));
Output:
Sara -- Design
Cahal -- Backend
Team never rewrites its ticket. It reads people, hands each entry to Welcome as that entry's own props, and folds the returned lines together. One Welcome forme, two tickets, two impressions -- and the parent's array is untouched.
Technical Depth: Props Are One Read-Only Object
React always calls your component with exactly one argument: a single object holding every attribute the parent wrote in the JSX tag. <Welcome name="Sara" role="Design" /> becomes the call Welcome({ name: "Sara", role: "Design" }). There is no second parameter for a "second prop" -- everything arrives inside that one object, and destructuring is just how you name the pieces you want.
Ways to read the ticket
| Pattern | Meaning |
|---|---|
function C(props) | receive the whole props object |
function C({ a, b }) | destructure the named props a and b |
function C({ a = 1 }) | use 1 when a is undefined |
<C a={x} /> | pass the expression x as prop a |
<C {...o} /> | spread object o's keys in as props |
children is just another prop
Whatever you place between a component's opening and closing tags arrives as the prop named children. <Panel>Save early</Panel> calls Panel({ children: "Save early" }). It is an ordinary prop with a reserved name, so it can be read and composed like any other.
function Panel({ title, children }) {
return "[" + title + "] " + children;
}
console.log(Panel({ title: "Note", children: "Save early" }));
Output:
[Note] Save early
Defaults fill only what is missing
A default value on a destructured prop is applied when that prop is undefined -- not when it is present but falsy. This is ordinary JavaScript default-parameter behavior, and it is a frequent source of surprise.
function Price({ amount, currency = "USD" }) {
return currency + " " + amount.toFixed(2);
}
console.log(Price({ amount: 9 }));
console.log(Price({ amount: 9, currency: "EUR" }));
Output:
USD 9.00
EUR 9.00
The first call omits currency, so the default "USD" fills in; the second supplies "EUR", which wins. Note that passing currency={null} explicitly would NOT trigger the default -- null is a value, and defaults only replace undefined.
Spreading a ticket, then overriding one line
Spreading an object into a tag copies its keys in as props, left to right; a prop written after the spread overrides the spread copy. Order is the whole rule.
function Chip({ label, count }) {
return label + ":" + count;
}
const data = { label: "open", count: 5 };
console.log(Chip({ ...data }));
console.log(Chip({ ...data, count: 0 }));
Output:
open:5
open:0
Composition: locking small formes into a page
A page is not one giant forme; it is small formes -- a headline, a body block, a footer rule -- locked side by side. Components compose the same way: a big view is built by nesting small components, each rendering a slice and handing the rest down. Modeled as pure functions, three tiny components fold into one list.
function Avatar({ name }) {
return "(" + name[0] + ")";
}
function UserRow({ user }) {
return Avatar({ name: user.name }) + " " + user.name + " -- " + user.status;
}
function UserList({ users }) {
return users.map((u) => UserRow({ user: u })).join("\n");
}
console.log(UserList({ users: [
{ name: "Ada", status: "online" },
{ name: "Ben", status: "away" },
] }));
Output:
(A) Ada -- online
(B) Ben -- away
Each component owns one job -- Avatar the initial, UserRow one line, UserList the loop -- and none of them reaches into another's data. That is the whole discipline: build small, pass props down, compose.
Functions are valid props, and that is how data flows back up
Props flow strictly downward: a parent hands data to a child, never the reverse. To let a child report something upward, the parent passes a function as a prop, and the child calls it. The function is still just a read-only ticket entry -- the child invokes it, it does not rewrite it.
function actionLabel({ label, transform }) {
return label + " -> " + transform(label);
}
console.log(actionLabel({ label: "Save", transform: (l) => l.toUpperCase() }));
Output:
Save -> SAVE
Real World Example: A Leaderboard That Rewrites Its Own Order
Here is a Leaderboard component that takes a title and a scores array as props and renders a ranked list. It is modeled as a pure string-returning function so its output can be traced exactly. Watch what happens to the parent's data.
function Leaderboard({ title, scores }) {
const ranked = scores.sort((a, b) => b.points - a.points);
const lines = ranked.map((s, i) => (i + 1) + ". " + s.name + " " + s.points);
return title + "\n" + lines.join("\n");
}
const season = [
{ name: "Ada", points: 40 },
{ name: "Ben", points: 90 },
{ name: "Cid", points: 70 },
];
console.log(Leaderboard({ title: "Top players", scores: season }));
console.log("raw order after render: " + season.map((s) => s.name).join(", "));
Output:
Top players
1. Ben 90
2. Cid 70
3. Ada 40
raw order after render: Ben, Cid, Ada
The rendered block reads correctly: the three players are ranked by points in descending order, numbered one through three, exactly as a scoreboard should. On the surface this component looks like a clean, pure function of its props -- it reads title and scores, derives a ranking, and returns text. If you only looked at the first four lines of output you would sign off on it.
The derivation itself is honest. ranked.map(...) builds each line from the entry's own fields, the numbering comes from the map index, and nothing about the displayed ranking depends on hidden state. Given the same props, the visible list is always the same, which is what a component should promise.
The last line of output is the confession. season was defined in source order Ada, Ben, Cid, yet after the component ran it reads Ben, Cid, Ada. Rendering silently reordered the parent's array. The culprit is scores.sort(...): Array.prototype.sort sorts in place and returns the same array, and scores is the very reference the parent still holds. The component pencilled a correction onto the job ticket, and the parent downstream now reads the altered order.
That is the real design flaw: Leaderboard mutates a prop. Props are read-only precisely so that rendering a child cannot reach back and change a parent's data, and this component quietly breaks that contract. The fix is one character of intent -- copy before you transform: const ranked = [...scores].sort(...), which sorts a throwaway clone and leaves season in its original order. The limits do not end there: the component also assumes every entry has numeric points and a name, and it will happily render undefined for a malformed ticket rather than flagging it.
Applying the one-character fix makes the contract hold. Cloning scores before sorting means the ranking is derived from a throwaway copy while the parent's array keeps its original order, so the same run now ends honestly.
function Leaderboard({ title, scores }) {
const ranked = [...scores].sort((a, b) => b.points - a.points);
const lines = ranked.map((s, i) => (i + 1) + ". " + s.name + " " + s.points);
return title + "\n" + lines.join("\n");
}
const season = [
{ name: "Ada", points: 40 },
{ name: "Ben", points: 90 },
{ name: "Cid", points: 70 },
];
console.log(Leaderboard({ title: "Top players", scores: season }));
console.log("raw order after render: " + season.map((s) => s.name).join(", "));
Output:
Top players
1. Ben 90
2. Cid 70
3. Ada 40
raw order after render: Ada, Ben, Cid
The rendered ranking is byte-for-byte the same as before -- the display never depended on the mutation -- but the final line now reads Ada, Ben, Cid, proving the parent's data survived the render untouched. A pure component and an unharmed prop: that is the target every time.
Common Mistakes
- Mutating a prop or a prop's array/object. Calling
.sort(),.push(), or assigning toprops.xchanges data the parent still owns. Copy first with[...arr]or{ ...obj }before transforming.
- Expecting positional arguments.
function C(a, b)does not receive two props; it receives the whole props object asaandundefinedasb. Every prop arrives inside one object.
- Reading a prop by the wrong name. A typo like
props.titelis not an error -- it is silentlyundefined, so the value just vanishes with no warning.
- Assuming children is always a string.
props.childrenmay be a string, a single element, an array of elements, or nothing. Code that concatenates it as text breaks the moment a real element is passed.
- Passing null and expecting the default. A destructured default fills only
undefined.<C value={null} />keepsnull; the default never fires.
- Drilling one prop through many layers. Threading a value down through five components that do not use it is a smell; reach for composition or context instead.
- Recreating object and array props every render.
<C style={{ margin: 0 }} />builds a brand-new object each render, so a memoized child sees a "changed" prop every time.
- Lowercasing a component name.
<welcome />is treated as an HTML tag, not your component; custom components must start with a capital letter.
- Copying a prop into a variable and reassigning it to "update" it. Reassigning a local does nothing visible; to change what renders you need state, not a rewritten input.
Best Practices
- Destructure props in the signature.
function C({ name, role = "guest" })documents exactly what the component consumes and sets defaults in one place.
- Treat props as read-only. If you must transform an array or object prop, clone it first (
[...scores]) so the parent's data is never disturbed.
- Name props for meaning, not markup.
isDisabledandvarianttravel better thanredBorder; the name should survive a restyle.
- Prefer composition and children over deep drilling. Let a parent pass rendered content in as
childrenrather than forwarding a dozen leaf props.
- Give a component the minimum props it needs. Passing a whole object when the child uses one field couples them needlessly.
- Keep prop identity stable when it matters. Hoist constant objects and arrays out of render, or memoize them, so memoized children are not defeated.
- Declare prop shapes with types. A TypeScript interface (or PropTypes) turns a wrong-name or wrong-type prop into a caught error instead of a silent
undefined.
- Keep components pure. Same props in, same output out, and never write to the inputs -- that is what makes them reusable like a forme.
Practice Exercises
- Fix the
Leaderboardflaw: change one line so the ranking is derived from a copy ofscores, then confirm thatseasonstill reads Ada, Ben, Cid after the component runs.
- Write a
Moneycomponent with a destructuredcurrency = "USD"default. Call it once withoutcurrency, once with"EUR", and once withnull, and predict which two calls print "USD" before running it.
- Build a
Panelthat takes atitleprop and renderschildren. Call it with a plain string child and hand-trace the returned line, then explain whatchildrenwould hold if you nested another component inside instead.
- Take three near-identical component calls that differ only in their props and collapse them into a single
.mapover a data array, keeping the printed output byte-for-byte identical.