Unays Learn ยท React

React for beginners: components, props and state

React is a JavaScript library for building user interfaces out of small, reusable pieces called components. This tutorial explains the core ideas you need before reading any React project.

What React does

Without React you change the page by hand: find an element, change its text, add a class. As a page grows, keeping every element in step with your data becomes hard. React turns this around: you describe what the page should look like for the current data, and React updates the page whenever the data changes.

You need to know JavaScript first, especially functions, arrays and objects. If those are new, start with JavaScript basics and JavaScript arrays.

Components and JSX

A component is a JavaScript function whose name starts with a capital letter and which returns markup. The HTML-like syntax is called JSX:

function Welcome() {
  return (
    <section className="card">
      <h1>Hello, class!</h1>
      <p>Today we learn React.</p>
    </section>
  );
}
  • JSX uses className instead of class, because class is a JavaScript keyword.
  • A component returns one outer element. Wrap siblings in a <div> or an empty fragment <>...</>.
  • Put any JavaScript expression inside curly braces: <p>2 + 2 = {2 + 2}</p>.

Passing data with props

Props are the inputs of a component, written like HTML attributes. The component receives them as one object, usually unpacked with destructuring:

function Lesson({ title, minutes }) {
  return <li>{title} ({minutes} min)</li>;
}

function LessonList() {
  return (
    <ul>
      <Lesson title="Variables" minutes={10} />
      <Lesson title="Functions" minutes={15} />
    </ul>
  );
}

Text props can use quotes; numbers and other values need curly braces. A component should never change its own props: they belong to the parent that passed them.

Changing values with state

Values that change while the page is open, such as a counter or the text in a box, live in state. The useState hook gives you the current value and a function to change it:

import { useState } from "react";

function Counter() {
  const [count, setCount] = useState(0);
  return (
    <button onClick={() => setCount(count + 1)}>
      Clicked {count} times
    </button>
  );
}

Calling setCount tells React the data changed, so React runs the component again and updates only the parts of the page that differ. Never change state directly (count = count + 1): React would not notice.

Project: a small tasks app

Lists are rendered with the array method map. Each item needs a stable, unique key so React can tell items apart:

import { useState } from "react";

export default function Tasks() {
  const [tasks, setTasks] = useState(["Read chapter 3"]);
  const [text, setText] = useState("");

  function addTask(event) {
    event.preventDefault();
    if (!text.trim()) return;
    setTasks([...tasks, text.trim()]);
    setText("");
  }

  return (
    <form onSubmit={addTask}>
      <label>
        New task
        <input value={text} onChange={e => setText(e.target.value)} />
      </label>
      <button>Add</button>
      <ul>
        {tasks.map((task, i) => <li key={i + task}>{task}</li>)}
      </ul>
    </form>
  );
}

[...tasks, newTask] builds a new array instead of changing the old one, which is how React expects state to be updated. In a real app, give each task an id and use that as the key.

Running React and next steps

JSX must be compiled before a browser can run it, so React projects use a build tool. A common way to start one on your own computer is npm create vite@latest and choosing the React template; the official guide at react.dev/learn walks through it. To practise the JavaScript ideas behind React first, build the plain JavaScript to-do list and compare it with the React version above.

This tutorial was written with AI assistance and reviewed by Unays. The runnable code examples were tested before publishing. Spotted a mistake? Tell us at [email protected].