ReactFundamentalsbeginner

React JS Tutorial: Step-by-Step Guide

Build a React task app step by step and learn components, props, state, forms, effects and common fixes.

Published
TL;DR – Quick Answer

React is a JavaScript library that builds a screen from components. Learn components and props first, then state, events, lists, forms and effects. This guide uses all of them in one task app.

On This Page

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, not class.

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 calls save while React draws the page. Write onClick={save} or onClick={() => 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:

  1. text stores what is in the input. This is a controlled input because React holds its value.
  2. addTask stops the normal form reload and adds a new object.
  3. toggleTask makes a new array. Only the matching task gets a new object.
  4. removeTask keeps every task except one.
  5. left is calculated from tasks. 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:

  1. Add filters for all, done and pending tasks.
  2. Let the user edit a task title.
  3. Move each row into a TaskItem component.
  4. Load data from an API and show an error if it fails.
  5. Add two pages with React Router.
  6. 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?
Yes. Know functions, objects, arrays, map, filter, destructuring, modules and promises. You do not need every JavaScript topic before you start.
Is React a framework or a library?
React is a library for user interfaces. A framework built with React can add routing, data loading, server features and a set project structure.
Which tool should I use to create a React app?
Vite is a simple choice for learning a browser-based app. A React framework is useful when you need built-in routing and server features. Create React App is deprecated for new apps.
Why does a React list need keys?
A key helps React match each item between updates. Use a stable ID from the data. An index can cause problems when items move or are removed.
When should I use useEffect?
Use it to connect a component to something outside React, such as browser storage or a timer. Do not use it for values that you can calculate from props or state.

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

Apply for Demo Class →
Siva Prasad Galaba
Founder, CodeBegun · Staff Engineer

Founder of CodeBegun. 15+ years building Java systems at companies like Crunchyroll. Teaches Java, Spring Boot and system design the way the industry actually works, and mentors students through projects, mock interviews and placement preparation.

Technically reviewed by CodeBegun Technical TeamLast reviewed 3 December 2026 LinkedIn
Chat with us