Sub topic 1
Setting up and writing your first components
This chapter creates the project and writes the first components of the to-do app. By the end you will have a running app that renders a heading and a static list, built from function components and JSX.
Prerequisites
This chapter assumes you have:
- JavaScript fundamentals: functions, arrays, objects, destructuring, and spread syntax.
- Familiarity with the array methods
mapandfilter. - Basic knowledge of HTML forms and elements.
- Node.js and npm installed.
You do not need prior React experience.
Creating the project
Vite is a build tool that starts a React project quickly and serves it with fast reloads. Run these commands in a terminal:
npm create vite@latest todo-app -- --template react
cd todo-app
npm install
npm run devVite prints a local address, usually http://localhost:5173. Open it to see the starter page. The files that matter now are index.html, src/main.jsx (which mounts React into the page), and src/App.jsx (the root component).
What JSX is
JSX is a syntax that lets you write markup inside JavaScript. A build step turns each tag into a plain JavaScript call, so the browser never sees JSX. A few rules differ from HTML:
- A component returns one root element. Wrap siblings in a parent element or in a fragment (
<>...</>). - Attributes use camelCase names, and
classbecomesclassName. - Curly braces
{}embed any JavaScript expression, such as a variable or a function call. - Every tag must be closed, including
<input />.
Writing a function component
A function component is a JavaScript function that returns JSX. Its name must start with a capital letter, because React treats lowercase tags such as <li> as built-in HTML elements.
Replace the contents of src/App.jsx with this:
export default function App() {
const title = "To-do";
const tasks = ["Buy milk", "Write chapter", "Walk the dog"];
return (
<main>
<h1>{title}</h1>
<ul>
{tasks.map((task) => (
<li key={task}>{task}</li>
))}
</ul>
</main>
);
}Save the file and the page updates without a reload. The {title} expression inserts a string. The tasks.map(...) call turns an array of strings into an array of <li> elements, which React renders in order. The key attribute helps React track list items. Chapter 6 covers keys properly. For now, the task text works as a key because these strings are unique.
Your first custom component
Components are reusable pieces of UI. Move the list item into its own component:
function TaskRow({ title }) {
return <li>{title}</li>;
}
export default function App() {
const tasks = ["Buy milk", "Write chapter", "Walk the dog"];
return (
<main>
<h1>To-do</h1>
<ul>
{tasks.map((task) => (
<TaskRow key={task} title={task} />
))}
</ul>
</main>
);
}TaskRow receives its input as one object and destructures title from it. That object holds props, which the next chapters explain in detail.
Common errors
| Symptom | Likely cause |
|---|---|
| `Adjacent JSX elements must be wrapped` | The component returns two root elements. Wrap them in a parent or a fragment. |
| A custom tag renders as an unknown HTML element | The component name starts with a lowercase letter. |
| `class` has no effect, with a console warning | Use `className` in JSX. |
| Nothing renders and no error appears | A function body with braces is missing `return`. |
Exercise
Why does function taskRow() { return <li>Buy milk</li>; } fail when you write <taskRow /> in JSX?
Show answer
React treats lowercase tag names as built-in HTML elements, so it looks for an HTML element called taskRow. Component names must start with a capital letter, as in TaskRow.
Exercise
Add a fourth task to the tasks array in App, and rewrite the heading so it shows how many tasks there are, for example "To-do (4)". Which JSX feature do you use?
Show answer
Add a string to the array, then change the heading to <h1>To-do ({tasks.length})</h1>. Curly braces embed a JavaScript expression in JSX.