React is a JavaScript library for building user interfaces from small parts called components. In this React JS tutorial, you will build one task app step by step: add a task, mark it done, remove it and save the list in the browser. You will also learn the rules that stop common bugs.
You should know functions, arrays, objects and modules in JavaScript. If map and the spread syntax are new, read JavaScript array methods first.
React JS tutorial setup
Install a current Node.js LTS version. Then run these commands in a terminal:
npm create vite@latest task-app -- --template react
cd task-app
npm install
npm run dev
Vite prints a local address, usually http://localhost:5173. Open it in your browser. Edits to your files then show up without a manual reload.
Only three files matter at first:
| File | Its job |
|---|---|
index.html |
Has one empty element with the ID root |
src/main.jsx |
Starts React inside that element |
src/App.jsx |
Holds your main component |
Old lessons use Create React App. The React team deprecated it for new apps in February 2025, so use a build tool or a framework instead. The Vite setup guide explains each file.
Step 1: Components, JSX and props
A component is a function that returns what should appear. JSX lets you write HTML-like code in JavaScript. Replace src/App.jsx with this:
function TaskItem({ title, done }) {
return (
<li>
{title} {done ? "(done)" : "(pending)"}
</li>
);
}
export default function App() {
return (
<main>
<h1>My tasks</h1>
<ul>
<TaskItem title="Learn JSX" done={true} />
<TaskItem title="Learn state" done={false} />
</ul>
</main>
);
}
The page shows this text:
My tasks
Learn JSX (done)
Learn state (pending)
title and done are props. They are inputs from the parent. A component must not change its own props.
Remember four JSX rules:
- Start component names with a capital letter.
- Return one parent element or a fragment,
<>...</>. - Put JavaScript expressions inside
{}. - Write
className, notclass.
Step 2: State and events
State is a component's memory. When you set new state, React calls the component again and updates the screen. This counter is the smallest useful example:
import { useState } from "react";
export default function Counter() {
const [count, setCount] = useState(0);
return (
<button onClick={() => setCount((old) => old + 1)}>
Clicked {count} times
</button>
);
}
The first screen shows Clicked 0 times. One click changes it to Clicked 1 times.
useState(0) returns the current value and a function that changes it. Use the function form, (old) => old + 1, when the next value depends on the old one.
Common mistake: Do not write
onClick={save()}. It callssavewhile React draws the page. WriteonClick={save}oronClick={() => save(id)}to call it on a click.
Step 3: Build the task app
Now put lists, a form and state together. Replace App.jsx again:
import { useState } from "react";
export default function App() {
const [tasks, setTasks] = useState([]);
const [text, setText] = useState("");
function addTask(event) {
event.preventDefault();
const title = text.trim();
if (title === "") return;
const task = { id: crypto.randomUUID(), title, done: false };
setTasks((old) => [...old, task]);
setText("");
}
function toggleTask(id) {
setTasks((old) =>
old.map((t) => (t.id === id ? { ...t, done: !t.done } : t))
);
}
function removeTask(id) {
setTasks((old) => old.filter((t) => t.id !== id));
}
const left = tasks.filter((t) => !t.done).length;
return (
<main>
<h1>My tasks</h1>
<form onSubmit={addTask}>
<label htmlFor="task">New task</label>
<input
id="task"
value={text}
onChange={(e) => setText(e.target.value)}
/>
<button type="submit">Add</button>
</form>
<p>{left} left</p>
<ul>
{tasks.map((t) => (
<li key={t.id}>
<label>
<input
type="checkbox"
checked={t.done}
onChange={() => toggleTask(t.id)}
/>
{t.title}
</label>
<button onClick={() => removeTask(t.id)}>Remove</button>
</li>
))}
</ul>
</main>
);
}
Read the code in this order:
textstores what is in the input. This is a controlled input because React holds its value.addTaskstops the normal form reload and adds a new object.toggleTaskmakes a new array. Only the matching task gets a new object.removeTaskkeeps every task except one.leftis calculated fromtasks. It does not need its own state.
Each list item has a stable key. React uses it to match items between updates. An array index can cause wrong input values when items move or disappear.
Pro tip: Do not change state with
tasks.push(...). Make a new array or object, so React can tell that the value changed.
Step 4: Save data with useEffect
An effect connects your component to something outside React. Here that outside thing is browser storage. Put this code in src/useSavedTasks.js:
import { useEffect, useState } from "react";
const KEY = "tasks";
function loadTasks() {
try {
const saved = JSON.parse(localStorage.getItem(KEY));
return Array.isArray(saved) ? saved : [];
} catch {
return [];
}
}
export function useSavedTasks() {
const [tasks, setTasks] = useState(loadTasks);
useEffect(() => {
localStorage.setItem(KEY, JSON.stringify(tasks));
}, [tasks]);
return [tasks, setTasks];
}
At the top of App.jsx, add this import:
import { useSavedTasks } from "./useSavedTasks";
Then replace the tasks line inside App with:
const [tasks, setTasks] = useSavedTasks();
Add a task and reload the page. It is still there. useSavedTasks is a custom hook: a function that uses other hooks and lets you share their logic.
The [tasks] list tells React to run the effect after the first screen and whenever tasks changes. In development, Strict Mode may run an effect setup one extra time to expose missing cleanup. The save above is safe to repeat.
Do not use an effect to calculate left. Work that comes directly from state belongs in normal code. For more rules, read the React hooks introduction.
Step 5: Find and fix common errors
| What you see | Likely cause | Fix |
|---|---|---|
| Objects are not valid as a React child | You printed an object | Print one field, such as {task.title} |
| Too many re-renders | You set state while drawing | Move it to an event |
| Each child needs a unique key | A list item has no key | Use a stable ID |
| Screen does not update | You changed the old array | Return a new array |
| Effect keeps running | It changes its own dependency | Remove the effect or fix the logic |
Open the browser console first. Read the first error and the file name. Change one thing, then test again. The React Developer Tools extension also lets you see the props and state of each component.
React JS tutorial for experienced developers
If you already know Angular, Vue or plain JavaScript, focus on how React thinks:
- Screen from state: describe the screen for the current data. Do not find and edit elements by hand.
- State location: keep state in the closest parent that needs it. Pass values down and event functions down.
- Snapshots: state stays fixed for one draw. Calling a setter does not change the variable in the code that is already running.
- Server data: handle loading, errors, stale replies and caching. A data library or framework is often better than many effects.
- Speed: measure first. Then split work or use memoization where it helps.
Useful next topics are useReducer, context, route-level data, testing and server components in a framework. Study React performance optimization after you can measure a slow page.
Interview note: If asked why the screen did not change, explain whether the code set new state and whether it made a new object. That is stronger than saying "React did not refresh."
Learn React JS from scratch: what to do next
You can learn React JS from scratch by extending this same app. Do these in order:
- Add filters for all, done and pending tasks.
- Let the user edit a task title.
- Move each row into a
TaskItemcomponent. - Load data from an API and show an error if it fails.
- Add two pages with React Router.
- Write tests for adding and removing a task.
This way to learn React JS step by step shows you why each new tool is needed. A React JS tutorial for beginners is done when you can rebuild the task app without looking.
Want a second project? Build the student list React app, which fetches data from an API. Then explain both projects using the React fresher questions.
Frequently Asked Questions
Do I need JavaScript before React?
Is React a framework or a library?
Which tool should I use to create a React app?
Why does a React list need keys?
When should I use useEffect?
Want to Build Your Career in Java Full Stack with AI?
Join CodeBegun and train with working industry engineers — Check the Java Full Stack training details

