Sub topic 1
Components as Functions That Return JSX
Prerequisites
This chapter assumes you know basic JavaScript: functions, objects, destructuring, and arrow functions. It also assumes you know HTML and can run a React project, for example one created with Vite. You don't need to know props yet. They start in the next chapter.
The working model
A React component is a JavaScript function that returns a description of what should appear on screen. That description is written in JSX, a syntax that looks like HTML but is compiled to JavaScript function calls. React calls your function, reads the returned value, and updates the page to match.
function Welcome() {
return <h1>Hello, world!</h1>;
}
export default function App() {
return (
<main>
<Welcome />
<Welcome />
</main>
);
}Welcome is a component. App uses it twice, and each use produces its own <h1>. Components are the unit of reuse in React. Props, which later chapters cover, are how each use gets different data.
What JSX is
JSX is not a string and not HTML. Each tag becomes a call that creates a plain object describing an element. The two forms below are equivalent after compilation:
const a = <h1 className="title">Hi</h1>;
const b = React.createElement('h1', { className: 'title' }, 'Hi');Because JSX compiles to JavaScript, it follows a few rules that differ from HTML:
- Use
classNameinstead ofclass, andhtmlForinstead offor. - Close every tag. Write
<br />and<input />, not<br>. - Return one root element. Wrap siblings in a parent element or in a fragment, written
<>...</>. - Put JavaScript expressions inside curly braces, such as
{2 + 2}or{user.name}.
function Total() {
const price = 20;
const quantity = 3;
return (
<>
<p>Price: {price}</p>
<p>Total: {price * quantity}</p>
</>
);
}Naming and usage rules
React tells components apart from plain HTML elements by the first letter of the tag name.
| Tag | React treats it as |
|---|---|
| `<div />` | A built-in HTML element |
| `<Welcome />` | A component named `Welcome` |
| `<welcome />` | An HTML element called `welcome`, which is not what you want |
Component names must start with a capital letter. A lowercase name makes React look for an HTML element instead of your function.
Common errors
Most first problems with components come from a short list of mistakes.
- Lowercase component name.
function welcome()used as<welcome />renders nothing useful. Rename it toWelcome. - Calling the component like a function. Write
<Welcome />, not{Welcome()}. React must call the function itself so it can manage the component's lifecycle. - Defining a component inside another component. The inner function is a new component on every render, so React discards and rebuilds its output each time. Define components at the top level of the file.
- Returning more than one root.
return <h1>A</h1><p>B</p>;is a syntax error. Wrap the elements in a fragment. - Forgetting to return. A function with braces and no
returngives backundefined, and React renders nothing.
Where props come in
So far Welcome always says the same thing. React calls a component with one argument, an object holding whatever the parent supplied. That object is the props object. Reading it is the subject of the next chapter.
Exercise
This component never shows on the page as intended, and React treats <greeting /> as an HTML tag. What two changes fix it? function greeting() { return <h1>Hi</h1><p>Welcome</p>; }
Show answer
Rename the function to Greeting and use it as <Greeting />, because component names must start with a capital letter. Also wrap the two returned elements in a single root, such as a fragment: return (<><h1>Hi</h1><p>Welcome</p></>);.
Exercise
Why is {Welcome()} a poor substitute for <Welcome /> inside another component?
Show answer
Calling the function directly runs its code as part of the parent, so React doesn't treat Welcome as its own component. Using <Welcome /> lets React call it, track it, and manage it separately.