Skip to Content
React in a Week
Day 1 · JS → first component

What is React?

What React is, why it exists, and the two big ideas behind it - components, and describing the UI instead of changing the page step by step.

You now know enough JavaScript to start on React itself. Before writing any, it helps to see what React does for you, by looking at what life is like without it.

React is a JavaScript library for building user interfaces. It was created at Facebook (now Meta) and is open source. It does one job: it keeps what is on the screen in sync with your data. Routing, data fetching and other features come from other libraries or from frameworks built on top of React, such as Next.js, which you will use on Day 7.

Life without React

A web page is made of elements: headings, paragraphs, buttons, lists. In the browser, these elements live in a tree called the DOM (Document Object Model). Plain JavaScript can change the page by changing the DOM directly.

Here is how you would show a list of books with plain JavaScript, and then add one more:

vanilla.js
const books = ['The Hobbit', 'Dune'];

// 1. Find the place on the page
const list = document.getElementById('book-list');

// 2. Create one element per book and attach it
for (const title of books) {
  const item = document.createElement('li');
  item.textContent = title;
  list.appendChild(item);
}

// 3. Later, a new book is added. Update the data...
books.push('Emma');

// ...and do not forget to update the page by hand too
const newItem = document.createElement('li');
newItem.textContent = 'Emma';
list.appendChild(newItem);

// 4. And the counter elsewhere on the page
document.getElementById('book-count').textContent = books.length + ' books';

This style is called imperative: you give the browser step-by-step instructions. Find this, create that, attach it there.

It works for small pages. But every time your data changes, you must remember every place on the page that depends on it, and update each one correctly. Forget one, and the screen shows something out of date. As an app grows, this becomes the main source of bugs.

The React way: describe the result

With React, you do not write the steps. You write a description of what the screen should look like for the current data. React works out the steps.

Live code· Describe, don't instruct

Add another title to the books array, such as 'Beloved'. Both the list and the count update, and you did not touch either of them. You changed the data, and the description took care of the rest.

This style is called declarative: you declare what you want, not how to get there. The HTML-like code inside return (...) is called JSX, and it is the subject of the next lesson. (The key part is explained on Day 2.)

When your data changes later in the week (because someone clicks a button, types in a box, or a server replies), React calls your description again, compares the new result with the old one, and updates only the DOM elements that differ.

Components: the building blocks

The second big idea is components. A component is a reusable, self-contained piece of UI: a button, a book card, a search box, a whole page. In React, a component is a JavaScript function that returns JSX.

You build bigger components out of smaller ones, the same way HTML nests tags.

Live code· A tree of components

Header, BookCard and Footer are components, and App uses them like custom HTML tags: <Header />. Notice that BookCard is written once but used twice. Change the title inside BookCard and both cards change.

Together, these components form a tree, with App at the top:

App
├── Header
├── BookCard
├── BookCard
└── Footer

This is exactly the tree you will see in the Components tab of React DevTools. Thinking about an app as a tree of components, each with one job, is the core skill of React.

Deep diveReact, React DOM and the virtual DOM

React is split into packages. react contains the core: components, hooks and the logic for working out what changed. react-dom connects that to a web browser's DOM. That split is why the same React knowledge also works with React Native for phone apps, which draws real phone UI instead of web elements.

You may read that React uses a "virtual DOM". It means React keeps a lightweight description of your UI (the JSX results), compares the new description with the previous one, and applies only the differences to the real DOM. You never work with it directly, and you do not need to understand it to use React well.

Try it

Imperative or declarative?

For each snippet, decide whether it is imperative or declarative.

  1. document.querySelector('h1').textContent = `Hi ${name}`
  2. return <h1>Hi {name}</h1>;
  3. list.appendChild(document.createElement('li'))
  4. books.map((title) => <li>{title}</li>)
Show solution
  1. Imperative. It finds an element and changes it.
  2. Declarative. It describes what the heading should contain.
  3. Imperative. It creates an element and attaches it by hand.
  4. Declarative. It describes one list item per title, and React creates the elements.

A quick test: if the code mentions document or changes an element you looked up, it is imperative. In React components you almost never touch document directly.

Quick check

What is a React component?

Quick check

In React, when the data changes, what do you write to update the page?

Day 1 progress

0/8

On this page