Sub topic 2
Passing props and reading them in a component
This chapter assumes you know basic JavaScript (functions, objects, dot notation), HTML attributes, and what a React function component is. It also assumes you can run a React project, for example one created with Vite.
Props are a component's input
A function takes arguments and returns a value. A React component works the same way. The parent supplies the input, and the component returns JSX describing what to show. That input is called props, short for properties.
You pass props as attributes in JSX. React collects them into one plain object and hands it to the component as its first argument.
function Greeting(props) {
return <h1>Hello, {props.name}!</h1>;
}
export default function App() {
return <Greeting name="Ada" />;
}When App renders <Greeting name="Ada" />, React calls Greeting({ name: "Ada" }). The curly braces in {props.name} tell JSX to evaluate a JavaScript expression. The page shows "Hello, Ada!".
Passing several props
Each attribute becomes one property on the object. Order does not matter, and the component reads only the ones it needs.
function UserBadge(props) {
return (
<p>
{props.name} ({props.role})
</p>
);
}
export default function App() {
return (
<div>
<UserBadge name="Ada" role="Engineer" />
<UserBadge name="Grace" role="Admiral" />
</div>
);
}Each UserBadge gets its own props object. The two badges do not share data, so one component definition can render many different results. That is the main reason props exist.
Rules for attribute names and values
- The attribute name becomes the property name exactly, including case.
userNamearrives asprops.userName, notprops.username. - A quoted value, such as
name="Ada", is always a string. - Component names must start with a capital letter.
<greeting />is treated as an HTML tag, while<Greeting />is treated as your component. - Props flow one way, from parent to child. A child cannot reach up and read its parent's variables.
Later chapters cover non-string values and destructuring. For now, remember that a quoted attribute gives you a string.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Output shows nothing where the value should be | Name mismatch, such as passing `name` but reading `props.username` | Match the attribute name and the property name exactly |
| `Cannot read properties of undefined` | You read a nested value, such as `props.user.name`, but the parent never passed `user` | Pass the prop, or check for it before reading |
| Component renders as an unknown lowercase HTML element | Lowercase component name | Capitalize the function and the tag |
| `props` is `{}` | The parent passed no attributes | Add them where you render the component |
| Value looks like the text `{props.name}` | Braces are missing or placed inside a quoted string | Put the expression in braces, outside quotes |
A quick way to debug any of these is to log the whole object at the top of the component.
function Greeting(props) {
console.log(props);
return <h1>Hello, {props.name}!</h1>;
}The browser console shows exactly which properties arrived. If a property is missing there, the problem is in the parent's JSX, not in the child.
Summary of the working model
- The parent writes attributes on the component's tag.
- React gathers them into one object.
- React calls the component function with that object as its first argument.
- The component reads values with dot notation and returns JSX.
Exercise
A parent renders <Price amount="20" currency="USD" />. The component is function Price(props) { return <p>{props.value} {props.currency}</p>; }. What does it display, and what is the fix?
Show answer
It displays only " USD" because props.value is undefined and renders as nothing. The parent passed amount, not value. Change the component to read props.amount, or change the attribute to value. Logging props with console.log(props) shows { amount: "20", currency: "USD" } and reveals the mismatch.