Components & props
Split UI into components, pass data down with props, children and default values.
Hard-coded components aren't very reusable. Props (short for properties) let a parent pass data into a child — just like arguments to a function. Together with composition, props are how you build big interfaces from small, reusable pieces.
Components are functions#
A component is a JavaScript function that returns JSX. Its name must start with a capital letter:
This always shows "JavaScript". To make it reusable, give it inputs.
Passing and reading props#
Props are written like HTML attributes and arrive as one object — the component's first parameter:
- Strings can use quotes:
title="Java". - Everything else uses braces:
lessons={34},featured={true},tags={["web"]},onSelect={handleSelect}. - A prop with no value is
true:<CourseCard featured />.
Destructuring props#
It's idiomatic to destructure right in the parameter list — and to set default values there:
The defaults apply when a prop is missing or undefined. (The old Component.defaultProps syntax was removed for function components in React 19.)
Passing objects and arrays#
Spreading ({...course}) is convenient but hides which props a component actually uses. Use it deliberately.
Props are read-only#
A component must never modify its props:
React components must behave like pure functions with respect to their props: same inputs → same output, with no side effects during rendering. Values that change over time live in state (coming soon), usually in a parent that passes them down as props.
The children prop#
Whatever you nest between a component's tags arrives as children:
children is how you build layout components, cards, modals, panels and page shells — components that provide a frame and let the caller decide the contents.
Any prop can hold JSX — sidebar above works like a second, named "slot".
Passing functions as props#
Data flows down via props. To let a child tell its parent something happened, the parent passes a function:
By convention, props that receive event handlers are named onSomething (onLike, onSelect, onClose), and the functions that handle them are named handleSomething.
Splitting a UI into components#
How do you decide what becomes a component? Look for pieces that:
- repeat (cards in a grid, rows in a table),
- have a clear single responsibility (a search box, a price tag),
- or make a big component easier to read.
Each piece is small, named and reusable — Avatar can now appear anywhere.
Exporting and importing components#
Put each component in its own file and import it where needed:
Named exports (export function Avatar) are common for components; Vite's starter uses export default for App. Either works — be consistent.
Don't define components inside components#
Always define components at the top level of a module.
Common mistakes#
- Lowercase component names:
<courseCard />is treated as an unknown HTML tag. - Forgetting braces for non-string props:
lessons="34"passes a string. - Mutating props or objects received via props.
- Calling a component like a function (
{CourseCard()}) instead of rendering it (<CourseCard />) — hooks inside it would break. - Passing
onClick={handleLike()}(calls it during render) instead ofonClick={handleLike}.
What's next#
Next: making components respond to the user with event handlers.
Check your understanding
Quick quiz
1.How do props flow in React?
2.Can a component change its own props?
3.What is the special
childrenprop?
Finished reading?
Mark this lesson complete to track your progress.