Day 1: JavaScript to your first component
Learn the modern JavaScript React relies on, then write JSX and build your first components.
Today has two halves. In the morning you learn the JavaScript that React code uses on nearly every line: arrow functions, destructuring, spread, map and filter, and modules. In the afternoon you put it to work: you learn what React is, how JSX works, and you write your first components.
Why start with JavaScript? Because most "React problems" beginners hit are really JavaScript problems in disguise. A component is a JavaScript function, and once the JavaScript feels familiar, React becomes much easier to follow. If you already know modern JavaScript well, try the challenge at the end of each JS lesson. If you solve it easily, move on.
Time plan
Plan for about five hours, including breaks.
| Lesson | Time | What you'll do |
|---|---|---|
| Variables, functions and conditions | 40 min | const, arrow functions, template literals, the ternary and && |
| Objects, arrays, destructuring and spread | 40 min | Model a book as an object, unpack it, and copy it safely |
| Array methods | 35 min | Transform and search a reading list with map, filter and find |
| Modules | 25 min | Split code into files with import and export |
| What is React? | 25 min | Compare step-by-step DOM code with React's declarative style |
| JSX | 45 min | Learn the rules of JSX and use curly braces |
| Your first component | 40 min | Define, nest and import components |
| Project checkpoint | 30 min | Build the BookCard component in your own project |
What you'll build today
By the end of today, your Reading List app shows a heading and two book cards, built from a BookCard component that lives in its own file.
Both cards show the same book, because BookCard has its title and author written directly inside it. That is on purpose. Tomorrow you will learn props, which let each card show a different book.
Today's lessons
Variables, functions and conditions
const, arrow functions, template literals, ternary and &&
Objects, arrays, destructuring and spread
Group data, unpack it and copy it without changing the original
Array methods
map, filter and find: the tools React lists are built on
Modules
Named and default exports, and how to import each
What is React?
Components, and describing the UI instead of changing it step by step
JSX
One root, closed tags, className and curly braces
Your first component
Define, nest, export and import components
Time to bring today's ideas into your own project. Open the reading-list folder from Day 0 in VS Code, and start the dev server with npm run dev if it is not already running.
Create the BookCard component
In the src folder, create a new file called BookCard.jsx:
export function BookCard() {
return (
<article className="book-card">
<h2>The Hobbit</h2>
<p>by J.R.R. Tolkien</p>
</article>
);
}The className does nothing yet. You will add the styles for it on Day 2.
Use it in App
Replace the contents of src/App.jsx with:
import { BookCard } from './BookCard.jsx';
export default function App() {
return (
<>
<h1>My Reading List</h1>
<BookCard />
<BookCard />
</>
);
}App now returns three elements, so they are wrapped in a Fragment. BookCard is a named export, so it is imported with curly braces. The rest of the course uses named exports for components; only App stays a default export.
Check it in the browser
Save both files. Your browser should show the heading and two identical cards. Open the Components tab in React DevTools: you should see App with two BookCard children.
Stretch goal: add a Header component in src/Header.jsx that contains the <h1>, and use <Header /> in App instead.
If something goes wrong, compare your code with the sandbox below. In the sandbox the files are named .js; in your project they are .jsx, and the code inside is the same.
Day 1 progress