Components and Props
Chapter 1's Footer component was useful, but completely static — every instance of it renders the exact same text. Props (short for "properties") are how a parent component passes data into a child component, the same way a function takes arguments. This is what turns a one-off component into something genuinely reusable.
Passing Props from a Parent
Attributes written on a component tag — name="Philip" — become a single props object passed as the function's first parameter. Greeting reads props.name to render different text each time it's used, without needing to be rewritten. The two <Greeting /> elements above render "Hello, Philip!" and "Hello, Sam!" from the exact same component definition.
Destructuring Props
Writing { name, age } directly in the parameter list destructures the props object, pulling out name and age as standalone variables. This is the convention used in almost all real React code — it reads more clearly than repeating props. in front of every value, and makes it obvious at a glance exactly which props a component expects.
Passing Different Types of Data
A string prop uses plain quotes (title="Keyboard"), but any other JavaScript value — numbers, booleans, arrays, objects, even other components — needs curly braces instead: price={49.99}, not price="49.99" (which would pass the string "49.99" rather than an actual number).
Default Prop Values
Ordinary JavaScript default-parameter syntax works directly in a destructured props parameter: color = "blue" supplies a fallback whenever the caller doesn't pass that prop at all. Note the double curly braces in style={{ background: color }} — the outer pair is the JSX expression, the inner pair is a regular JS object literal for the style prop, which React expects as an object rather than a CSS string.
| Syntax | Passes... |
|---|---|
| name="Sam" | A string, "Sam" |
| age={28} | A number, 28 |
| active={true} | A boolean, true |
| onClick={handleClick} | A function reference (not called yet) |
Coding Challenges
Create a MovieCard component that destructures title and year props, rendering them as "Title (Year)". Use it three times in App with three different movies.
📄 View solutionCreate a Badge component that accepts a text prop and a color prop with a default value of "gray". Render it once with a custom color and once without one, confirming the default applies.
📄 View solutionCreate a StatusLabel component that accepts a boolean isOnline prop and renders "Online" or "Offline" using a ternary, with the text color also changing based on the same prop.
📄 View solutionChapter 2 Quick Reference
- Props — data passed from a parent component into a child, like function arguments
- Component tag attributes (
name="Sam") become a single props object - Destructuring —
function Comp({ a, b })reads cleaner thanprops.a/props.b - String props use quotes; everything else uses curly braces for real JS values
- Default values —
{ color = "blue" }, same as default function parameters - Props are read-only from the child's side — they flow one way, parent to child
- Next chapter: state with useState — giving a component data it can change itself