Components and props
Writing function components, passing and reading props, why props are read-only, and what the children prop is for.
JSX syntax and compilationThe virtual DOM and reconciliation
Components and props
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A function component is just a JavaScript function that returns JSX; React calls it and renders whatever it returns.
- Props are the arguments a component receives from whoever renders it — passed as JSX attributes, read as a single object parameter (commonly destructured).
- Props are read-only from the receiving component's point of view: a component must never reassign or mutate the props object it's given, only use it.
- Data flows down through props by design — a component only knows what its parent chose to hand it, which is the mechanism behind the one-way data flow described earlier in this roadmap.
- children is just another prop, populated automatically from whatever JSX a component is given between its opening and closing tags, and it's how components wrap or lay out content they don't control the content of.
A component is a function
Strip away the ceremony, and a React component is nothing more exotic than a JavaScript function that returns JSX:
function Greeting() {
return <h1>Hello!</h1>;
}
React treats any function like this — name starting with a capital letter, returning JSX — as a component it can render. You use it in JSX exactly like a built-in element:
function App() {
return <Greeting />;
}
Nothing here is magic beyond ordinary function calls: App returns JSX containing <Greeting />, React resolves that to a call to the Greeting function, and substitutes whatever Greeting returns.
Passing data in: props
A component that only ever renders the same hardcoded output isn’t very reusable. Props (“properties”) are how a parent passes data into a component it renders, written as JSX attributes:
function App() {
return <Greeting name="Ada" />;
}
React packages every attribute you write into a single object and passes that object as the component’s one argument:
function Greeting(props) {
return <h1>Hello, {props.name}!</h1>;
}
In practice, almost everyone destructures that object directly in the function signature, since most components read several props and props.whatever everywhere gets noisy:
function Greeting({ name }) {
return <h1>Hello, {name}!</h1>;
}
Both versions behave identically — destructuring is a JavaScript-level convenience, not a different mechanism. (Destructuring syntax itself, if it’s unfamiliar, is plain JavaScript and is covered in the javascript roadmap; this lesson assumes it.) A component can take as many props as it needs:
function UserCard({ name, role, avatarUrl }) {
return (
<div className="user-card">
<img src={avatarUrl} alt={name} />
<h2>{name}</h2>
<p>{role}</p>
</div>
);
}
// used as:
<UserCard name="Ada" role="Engineer" avatarUrl="/ada.jpg" />;
Props are read-only
Here’s the rule that matters more than the syntax: a component must never modify the props it receives. Props belong to whichever component passed them down; treating them as mutable breaks the one-way data flow this roadmap’s first lesson introduced, and produces bugs that are hard to trace, because the value a component displays would then depend on which other code happened to run and mutate it first.
// Don't do this:
function UserCard({ name }) {
name = name.toUpperCase(); // mutating/reassigning a prop
return <h2>{name}</h2>;
}
// Do this instead — derive a new value, leave the prop untouched:
function UserCard({ name }) {
const displayName = name.toUpperCase();
return <h2>{displayName}</h2>;
}
The second version is functionally identical in output, but it never touches the name prop itself — it computes a separate value from it. This distinction matters more once props are objects or arrays, where mutating a nested field can silently affect the parent’s own copy of that data, since objects are passed by reference in JavaScript. If a component needs to display something different, the correct move is always to have the parent pass different data down — never to reach into what it was given and change it directly.
children: the prop you don’t name
There’s one prop every component receives automatically, without a parent ever writing children={...} explicitly: whatever JSX is nested between a component’s opening and closing tags.
function Card({ children }) {
return <div className="card">{children}</div>;
}
// used as:
<Card>
<h2>Title</h2>
<p>Some content here.</p>
</Card>;
React collects the <h2> and <p> nested inside <Card>...</Card> and passes them to Card as props.children. This is the mechanism behind every “wrapper” or “layout” component you’ll build — a Modal, a Card, a PageSection — anything that needs to render some structure (a border, a header, padding) around content it doesn’t itself know or care about. Card has no idea whether it’s wrapping a paragraph, a form, or another component; it just renders children wherever that content belongs in its own markup.
Putting it together
A component that both accepts specific props and renders children is a common, useful combination:
function Panel({ title, children }) {
return (
<section className="panel">
<h3>{title}</h3>
<div className="panel-body">{children}</div>
</section>
);
}
// used as:
<Panel title="Account settings">
<SettingsForm />
</Panel>;
Panel controls its own structure (a titled section with a body wrapper) while staying agnostic about what actually goes inside — that’s children’s job, and title is an ordinary prop like any other.
What the video covers
Codevolution’s episode, from their current React 19 series, walks through passing and reading props on function components and explains why props are treated as read-only — the core of this lesson. It assumes you already know what a function component is (covered in the series’ dedicated “Components” episode, linked below, and in this lesson’s opening section) and moves on quickly to props patterns rather than spending much time on the children prop specifically, which this lesson’s last two sections cover in more depth.
Where this fits
With components and props in hand, the next two lessons cover what to actually put inside the JSX a component returns: conditional rendering, then rendering lists and keys.
Check yourself
What, mechanically, is a React function component?
An ordinary JavaScript function that takes a single object (conventionally destructured as named props) as its argument and returns JSX describing what should render. React calls it, reads the returned element tree, and handles rendering it.
If a parent renders <UserCard name="Ada" />, how does UserCard access that value?
React passes a single props object to UserCard, so it receives { name: "Ada" }. The function signature typically destructures it directly, as in function UserCard({ name }), making name available as a plain variable inside the function body.
Why shouldn't a component reassign a value inside the props object it receives?
Props belong to the parent that passed them down; mutating or reassigning them inside a child breaks the one-way flow of data and can produce inconsistent state, since the parent (and any other component sharing that same data) has no way to know a child changed it. If a component needs to change what's displayed, it should ask the parent to pass different data down, not alter what it was given.
What ends up inside a component's children prop, and when is it populated?
Whatever JSX is nested between a component's opening and closing tags when it's used, for example the text and elements between <Card> and </Card>. React automatically collects that nested content and passes it to the component as the children prop, without the parent having to name it explicitly.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.