Sub topic 2
Quick review: components, JSX, and props
This chapter assumes you can read basic JavaScript (objects, arrays, arrow functions) and HTML elements and attributes. It also assumes you have seen JSX before. The review covers the three ideas the rest of the tutorial uses: function components, JSX, and props.
Function components
A React component is a JavaScript function that returns markup. React calls the function each time it needs to know what that part of the screen looks like. Component names must start with a capital letter. React treats a lowercase tag such as <tr> as a built-in HTML element, and a capitalized tag such as <ProductRow /> as a call to your function.
JSX
JSX is a syntax that lets you write markup inside JavaScript. A build tool compiles it into plain function calls. It looks like HTML, with a few differences:
- Every tag must be closed, so write
<input />rather than<input>. - A component returns one root element. Wrap siblings in a parent element or a fragment (
<>...</>). - Attributes use camelCase names. Use
classNameinstead ofclass, andcolSpaninstead ofcolspan. - Curly braces switch back into JavaScript.
{product.price}inserts a value, and{cond ? a : b}chooses between two results. - The
styleattribute takes an object, so inline styles need two pairs of braces:style={{ color: 'red' }}. The outer pair is the JavaScript escape. The inner pair is the object.
Props
Props are the inputs to a component. A parent passes them as attributes, and the child receives them as one object. Destructuring in the parameter list is the usual way to read them.
function ProductRow({ product }) {
const name = product.stocked ? (
product.name
) : (
<span style={{ color: 'red' }}>{product.name}</span>
);
return (
<tr>
<td>{name}</td>
<td>{product.price}</td>
</tr>
);
}
export default function App() {
const apple = { category: 'Fruits', price: '$1', stocked: true, name: 'Apple' };
const pumpkin = { category: 'Vegetables', price: '$4', stocked: false, name: 'Pumpkin' };
return (
<table>
<tbody>
<ProductRow product={apple} />
<ProductRow product={pumpkin} /></table>
);
}App renders ProductRow twice with different product props Pumpkin row shows its name in red because stocked is false. The component's output depends only on its props, so the same props always give the same markup. This property is what makes a component predictable.
Props are read-only
A component must never change its own props. If ProductRow ran product.price = '$2', it would alter data that belongs to its parent, and other components that read the same object would see a change React knows nothing about. Treat props as a snapshot handed down by the parent. Data that changes over time is called state, and a later chapter covers it.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| A custom tag renders as an unknown HTML element | The component name starts with a lowercase letter | Rename it, for example `productRow` to `ProductRow` |
| "Adjacent JSX elements must be wrapped" | The component returns two siblings | Wrap them in a parent element or `<>...</>` |
| `[object Object]` appears on screen | An object was placed inside `{}` as text | Render one of its fields, such as `{product.name}` |
| A prop is `undefined` in the child | The parent used a different prop name | Match the attribute name to the destructured name |
| Style does not apply | A string was passed to `style` | Pass an object: `style={{ color: 'red' }}` |
Why this matters for the next step
The five-step process builds on these ideas. You will split the mockup into components, and each component will be a function of its props. The static version in the third step is built only from props. Knowing that data flows from parent to child through attributes makes the later discussion of state and callbacks easier to follow.
Exercise
In <span style={{ color: 'red' }}>{product.name}</span>, what do the double braces around color mean, and why does {product.name} use only one pair?
Show answer
The outer braces switch from JSX into JavaScript, and the inner braces are a JavaScript object literal, because style expects an object. {product.name} only needs to insert a value, so one pair of braces for the JavaScript escape is enough.
Exercise
A teammate writes function productRow({ product }) { ... } and renders <productRow product={apple} />. The browser shows an empty unknown element. What is wrong, and how do you fix it?
Show answer
The name starts with a lowercase letter, so React treats it as an HTML tag instead of a component. Rename the function to ProductRow and use <ProductRow product={apple} />.