Sub topic 1
Components are functions that return UI
Prerequisites
This chapter assumes you know modern JavaScript: functions, arrow functions, destructuring, and array map. It also assumes basic HTML elements and attributes, and ES modules (import and export). You don't need to know React yet.
The working model
A React component is a JavaScript function that returns a description of what should appear on screen. React calls the function, reads what it returns, and updates the browser to match.
function Greeting({ name }) {
return <h1>Hello, {name}!</h1>;
}
export default function App() {
return <Greeting name="Ada" />;
}This example has two components. Greeting receives an object of inputs (called props), pulls out name by destructuring, and returns markup. App returns <Greeting name="Ada" />, which tells React to call Greeting with { name: "Ada" }. The result is an <h1> that reads "Hello, Ada!".
The markup-like syntax is JSX. It looks like HTML, but it is JavaScript. Later chapters cover JSX rules in detail. For now, treat each JSX tag as an instruction to React.
Rules that make a function a component
Two rules cover most cases:
- The name starts with a capital letter. React treats
<Greeting />as a component and<greeting />as a plain HTML tag, which doesn't exist. - It returns something React can render. This is usually JSX, but it can also be a string, a number, or
null(which renders nothing).
You use a component by writing it as a tag. You don't call it yourself. Writing Greeting({ name: "Ada" }) directly skips React's bookkeeping, and this breaks features such as state later on.
How the pieces fit together
The diagram below shows how React turns your components into what the user sees.
React starts at the top component and works downward. Each function runs, returns JSX, and any components inside that JSX are called in turn. This continues until only plain HTML elements remain.
Common errors
| Mistake | What happens | Fix |
|---|---|---|
| Lowercase component name, such as `<greeting />` | React looks for an HTML tag called `greeting`, so nothing useful renders | Rename the function to `Greeting` |
| Forgetting `return` in a function with a block body | The component returns `undefined` and renders nothing | Add `return`, or use an arrow function with an expression body |
| Returning two sibling elements | JSX must have one root, so this is a syntax error | Wrap them in one element, or use a fragment (covered later) |
| Defining a component inside another component | React sees a new component type on every render and discards its state | Define components at the top level of the module |
Putting it together
Components can be exported from one file and imported into another. Each file usually holds one main component as its default export.
// Greeting.jsx
export default function Greeting({ name }) {
return <h1>Hello, {name}!</h1>;
}// App.jsx
import Greeting from './Greeting.jsx';
export default function App() {
return (
<div>
<Greeting name="Ada" />
<Greeting name="Grace" />
</div>
);
}The same component renders twice with different props, producing two separate headings. Reuse like this is the main reason to split a page into components.
Exercise
A teammate writes function profile() { return <p>Hi</p>; } and uses it as <profile />. Nothing from your component appears. What is wrong, and how do you fix it?
Show answer
The name starts with a lowercase letter, so React treats <profile /> as an HTML tag instead of a component. Rename the function to Profile and use <Profile />.
Exercise
Why should you write <Greeting name="Ada" /> instead of calling Greeting({ name: "Ada" }) inside JSX?
Show answer
React must call the component itself so it can track each component's state and manage its updates. Calling the function directly skips that tracking and breaks features such as state.