Picture an online shop page. It has the shopper's name in the top corner, a number on the cart icon, a list of products, and a note box for the seller. Every time some data changes, the part of the page that shows that data has to change too, and the other parts must be left alone. If you handle all of that yourself with plain JavaScript, the code quickly becomes hard to follow. React was made to handle this work.
React is a JavaScript library (a collection of ready-made code) for building web user interfaces. Meta, which was called Facebook at the time, made it and released it in 2013. With React, you split the screen into small pieces called components. Each component only says what it should look like for the data it has right now. Changing the page every time that data changes is React's job.
This way of working is like renovating a house. You give the builder a drawing of the house you want. The builder compares the drawing with the house as it is now, and rebuilds only the parts that are different. If only the colour of the door is different, only the door is repainted. The house is not torn down, and everything inside stays where it was. In React, the drawing is the UI you write in a component, the builder is React, and the house is the page that the browser is showing.
A library, not a framework
React handles one thing only: the user interface (UI). That is what a library is like: you call it for one job. A framework is bigger. A framework also decides how you move between pages, how you fetch data, even how the project's folders are arranged. Angular is an example of a framework. Next.js is a framework too, and it is built on top of React.
So if your React app needs to move between pages (routing), that feature comes from another library, such as React Router, or from a framework such as Next.js. On this track you learn React itself first.
Components: a UI made of small pieces
A component is a JavaScript function whose name starts with a capital letter and that returns what to show. This is the smallest component:
function App() {
return <h1>Hello React!</h1>
}
function App()makes a component calledApp.Appis usually the outermost component, the one that other components are placed in.return <h1>Hello React!</h1>returns what the component shows. This HTML-like writing is called JSX: a way to write a UI like HTML inside JavaScript. The next lesson covers the rules of JSX.- On the page you get one
<h1>heading that readsHello React!.
A big page is built from many components, such as Navbar, ProductCard and Cart. One component can be used many times, so the code for a product card is written only once.
Declarative: say the result, not the steps
Take a small shop page that has a greeting, the number of items in the cart, and a note box. With plain JavaScript, you rewrite the page's content yourself:
// The manual way: you change the DOM yourself
const name = "Budi"
const count = 2
document.querySelector("#root").innerHTML = `
<h1>Hello, ${name}!</h1>
<p>Items in cart: ${count}</p>
<input placeholder="Note">
`
With React, you only write what the page should look like:
// The React way: you describe the end result
function App() {
const name = "Budi"
const count = 2
return (
<div>
<h1>Hello, {name}!</h1>
<p>Items in cart: {count}</p>
<input placeholder="Note" />
</div>
)
}
- The first way is called imperative: you give the DOM orders, step by step. The DOM is the structure of page elements that the browser builds from the HTML.
- The second way is called declarative: you only write the end result.
{name}means "put the value of the variablenamehere". - The React code above never touches the DOM. React is what changes the DOM.
What React does every time the data changes
For example, you have already typed something into the note box, and then the shopper's name changes from Budi to Sari. How to make data that can change (it is called state) has a lesson of its own. Here, just compare what happens on the page with the two ways above:
The innerHTML way |
The React way | |
|---|---|---|
| Elements thrown away | 3 (h1, p, input) |
0 |
| Elements built new | 3 | 0 |
| What changes | everything inside #root |
1 piece of text: Budi becomes Sari |
| What you typed in the note box | lost | kept |
innerHTML throws every old element away, then builds new elements, the note box included. The new note box is empty, so what you typed is gone. React works in three steps:
- React runs
Appagain. - React compares the new result with the previous one.
- React changes only the part of the DOM that is different, which is one piece of text: the name.
The note box is still the same element, so what you typed stays.
The result of running App is called the render result. The render result that React keeps and compares is a plain JavaScript object that describes the UI, and it is often called the virtual DOM. Comparing objects is much less work than throwing away and rebuilding elements on the page.
Starting a React project
React projects are usually made with Vite, a tool that sets up the project and runs it on your computer. First install Node.js (a program that runs JavaScript outside the browser). Node.js includes npm, a tool that downloads packages of code. Then run these commands one at a time in the terminal (a window where you type commands):
npm create vite@latest
cd your-project-name
npm install
npm run dev
npm create vite@latestcreates a new project with the latest Vite. This command asks for the project name, then the framework. Vite's list calls React a framework too: choose React, then JavaScript.cd your-project-namemoves into the project's folder (use the project name you chose).npm installdownloads React and the other packages that the project needs.npm run devruns the app. After that, open in your browser the address thatnpm run devprints.- After React and JavaScript, Vite asks for a linter (a tool that checks your code): the first choice, Oxlint, is enough. The last question is
Install with npm and start now?, and Yes is already selected. If you answer Yes, Vite installs the packages and starts the app for you, so you do not need to typecd,npm installandnpm run dev. Answer No if you want to run those three commands yourself.
Other options for starting a React project:
| Way | When to pick it |
|---|---|
Vite (npm create vite@latest) |
Learning React, or a small app that is only a UI. This track uses Vite, because you are learning React itself. |
React Router (npx create-react-router@latest) or Next.js (npx create-next-app@latest) |
A full app with many pages, data loading and server code. For a new app, the React docs (react.dev) recommend starting with a framework like these. |
Expo (npx create-expo-app@latest) |
A mobile app for Android and iOS. |
Create React App (npx create-react-app) has been deprecated since February 2025. You will still see it in old tutorials, but do not use it for new projects.
In that project, the file index.html has <div id="root"></div>, and the file src/main.jsx puts App into that div:
import { createRoot } from "react-dom/client"
import App from "./App.jsx"
createRoot(document.getElementById("root")).render(<App />)
- The two
importlines load code from other files:createRootfrom the React package, andAppfrom its own file. createRoot(...)gives<div id="root">to React. From then on, React handles what is inside that div..render(<App />)shows theAppcomponent inside that div. In JSX, you use a component by writing it like a tag:<App />.- The file that Vite makes is a little longer: it has
import './index.css', and<App />is wrapped in<StrictMode>.<StrictMode>works only in development. There, React runs some code twice to help you find bugs, so do not be surprised when aconsole.logshows twice.
In the BelajarWeb exercise editor, this setup step is done for you. You only write function App(), and that App is what gets shown.
⚠️ Day-one misunderstandings about React
- Thinking React is a complete framework. React only handles the UI. Routing and data fetching come from other libraries, or from a framework built on React such as Next.js.
- Changing the DOM yourself in the part React manages. If you use
innerHTMLorappendChildinside#root, React and your code fight over the same elements. Change only the data, and let React change the DOM. - Adding
createRootin the exercise editor. The editor already putsAppon the page for you. It also has only one file, soimport App from "./App.jsx"fails with the messageFailed to resolve import(the imported file was not found). Just writefunction App(). You may write animportfrom"react", but you don't have to. - The
<h1>text being slightly different. The exercise checks for exactlyHello React!: a capital R, one space, and an exclamation mark at the end.
TL;DR: React is a JavaScript library (a collection of ready-made code) from Meta (called Facebook at the time, 2013) for building web UIs. It is not a framework, because it only handles the UI, while a framework also manages other things, such as moving between pages. The UI is split into components: functions whose name starts with a capital letter and that return JSX (HTML-like writing inside JavaScript), such as function App() { return <h1>Hello React!</h1> }. You only write the end result (the declarative way). Every time the data changes, React runs the component again and compares its result with the old one, then changes only the parts of the DOM (the structure of page elements) that are different. That is why what you typed in an input isn't lost. A new project is made with the command npm create vite@latest, and createRoot(...).render(<App />) puts App into <div id="root">.
⚡ A component is a function
Write:
function App() {
return <h1>Hello React!</h1>
}
Meaning: React is a JavaScript library (ready-made code) for building a web UI out of small pieces called components. A component is a function whose name starts with a capital letter and that returns what to show. The HTML-like writing inside it is called JSX. The result of this code is one <h1> that reads Hello React!.
In the exercise, the text must be exactly Hello React!: a capital R, one space, an exclamation mark at the end.
⚡ Say the result, not the steps
Write:
function App() {
const name = "Budi"
const count = 2
return (
<div>
<h1>Hello, {name}!</h1>
<p>Items in cart: {count}</p>
</div>
)
}
Meaning: This is the declarative way: you only write the end result of the UI, not the steps to build it. {name} means "put the value of the variable name here". Your code never touches the DOM (the structure of page elements in the browser). React changes the DOM for you.
⚡ Only what differs gets changed
Guess first: a page shows "Hello, Budi!" and a note box. You have typed something into the note box, then the name changes from Budi to Sari. Is what you typed lost?
It depends on how the page is changed. If the page is rewritten with innerHTML, what you typed is lost: every element is thrown away and built new, the note box included. With React, it stays, because only one piece of text is changed: Budi becomes Sari.
Meaning: Every time the data changes, React runs the App component again, compares the new result with the old one, then changes only the parts of the page that are different.
⚡ A library, not a framework
Guess first: a library is ready-made code for one job. A framework is bigger: it also decides how you move between pages, how you fetch data and how the project's folders are arranged. Is React a library or a framework?
A library. React only handles the UI.
Meaning: Moving between pages (routing) and fetching data come from other libraries such as React Router, or from a framework built on React such as Next.js.