Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 132 ยท Getting Started

Getting Started with React

Start learning React 19 with live examples. See what React is, how UI = f(state) works, write your first component in the browser and compare it with plain DOM code.

React is a JavaScript library for building user interfaces. You describe what the screen should look like for the current data, using small reusable pieces called components, and React makes the page match that description. When the data changes, you do not hunt down elements and patch them one by one. You describe the new screen again, and React works out the smallest set of changes to apply.

That one idea explains most of what makes React different, and it is the thread running through this whole tutorial. This lesson gives you the mental model, runs your first component right here in the page, and compares React with the plain DOM code you may already know. If you are not comfortable with modern JavaScript yet, the lesson The JavaScript You Need for React covers the parts that matter, and the JavaScript tutorial teaches the language from scratch.

What React is (and is not)

React is a library, not a full framework. Its job is the view layer: turning data into user interface and keeping that interface in sync when the data changes. It does not come with a router, a data-fetching system or a form validator. You add those from the ecosystem, or you use a framework built on React, such as Next.js or React Router in framework mode, that bundles those choices for you (Setting Up a React Project compares them).

React was created at Facebook (now Meta) and open-sourced in 2013. Today it is developed by Meta and a large community, and it is used by a huge number of websites and apps, from small tools to some of the largest products on the web. The same ideas power React Native, which renders to native mobile controls instead of the browser DOM (How React Works explains how one library can target both).

A few things React gives you:

  • Components. A component is a JavaScript function that returns a piece of UI. You build screens by nesting components, the way you nest HTML elements, but you can invent your own tags such as <ProductCard />.
  • A declarative model. You say what the UI is for a given state, not how to get from the old screen to the new one.
  • Hooks. Functions such as useState that let a component remember values and run code at the right moments.
  • A huge ecosystem. Routing, state management, forms, animation, testing and component libraries are all available.

And what React is not: it is not a language (components are written in JavaScript), it is not a styling system, and it does not decide how your server works.

The mental model: UI is a function of state

Here is the idea in one line:

TEXT
UI = f(state)

Your state is the data that can change while the app is running: the text in a box, the items in a cart, whether a menu is open. A component is a function that takes that data and returns a description of the screen. Give it the same data, you get the same description. When the state changes, React calls the function again with the new data, compares the new description with the old one, and updates only what differs.

Compare that with the way you might do it by hand: find the counter element, read its text, convert it to a number, add one, write it back. In React you only change the number. The screen follows.

This "describe, don't mutate" thinking is the biggest shift for newcomers. When something in your React app looks wrong, the question is rarely "which element did I forget to update?" It is "is my state right, and does my component describe the right screen for that state?"

Your first component

The editor below is a live React environment. It contains a complete program: a component named Greeting, and one line at the bottom that tells React where to show it. Press Run (or just edit the code; the preview updates as you type) and change the name.

Your first React component

Let us read it line by line.

  1. import { createRoot } from 'react-dom/client' brings in the function that connects React to a real page. react-dom is the package that knows how to draw React components into a browser.
  2. function Greeting() { ... } is a component. It is an ordinary function. Its name starts with a capital letter, which is required: React treats lowercase names such as div as built-in HTML tags and capitalized names as your own components.
  3. return <h1>Hello, {name}!</h1>; returns JSX, a syntax that looks like HTML inside JavaScript. The curly braces mean "insert the value of this JavaScript expression here". JSX Basics covers it fully.
  4. createRoot(document.getElementById('root')) takes an existing element in the page (here an empty <div id="root">) and makes it the root where React owns the screen. .render(<Greeting />) draws the component in it.

In a real project the page index.html contains that empty <div id="root"></div>, and the bundle you build contains your components. Our editor builds such a page for you every time you run the code.

Components can be nested and reused

A component can use other components, and it can use the same one many times with different props (inputs, like attributes on an HTML tag). This is where React starts to feel productive.

One component, three uses

Badge is written once and receives label and color as props. App is the parent; it calls for three badges. Change a color or add a fourth badge. The pieces are small, they have a name, and you can read the result at a glance: that is the payoff of components. We go deeper in Your First Components and Props.

Adding state: the screen follows the data

A component with only fixed output is like a static page. Interactivity comes from state. The useState hook gives a component a piece of memory and a function to change it:

JSX
const [count, setCount] = useState(0);

count is the current value (starting at 0). setCount(next) stores a new value and tells React to render the component again. Try it:

A counter driven by state

Click the button a few times and watch the console panel under the preview. The log line appears once for the first render, and then once more after every click. That is the whole cycle in miniature:

  1. The user clicks. The onClick handler calls setCount(count + 1).
  2. React schedules a new render of Counter with the new value.
  3. The function runs again from the top. This time useState returns the new number, so the JSX describes "You clicked 1 times."
  4. React compares the new description with the old one, sees that only one text node changed, and updates just that text in the real DOM.

You never wrote code to change the paragraph. You wrote once, and React keeps it right. This is the declarative model at work. We study it properly in How React Works and, much later, Rendering in Depth.

React compared with plain DOM code

To see why this matters, here is the same counter written two ways. The first is "vanilla" JavaScript manipulating the DOM directly. Run it and click the button.

The same counter without React

For one number this is fine, and honestly it is shorter. The trouble appears as the screen grows. Imagine the count also needs to show in a header badge, disable a button at 10, and turn a list item red when it is even. With direct DOM code, every place that changes count must remember to update every piece of screen that depends on it. Forget one and the UI is inconsistent, a bug that gets worse the larger the app is.

In React, you change the state in one place, and the component function describes the whole screen from it. The relationship between data and screen is written down once, as plain code, instead of being scattered through event handlers.

Plain DOM code React
Style Imperative: do these steps Declarative: here is the result
Source of truth The DOM itself (text in elements) State in your components
Updating You find and change elements You change state; React updates the DOM
Reuse Copy markup or build helpers by hand Components with props
Best for Small enhancements to a page Interfaces with a lot of changing state

React is not always the right tool. For a mostly static page with one small interactive widget, plain HTML, CSS and a few lines of JavaScript can be simpler and faster. React earns its weight when the interface has many parts that depend on shared, changing data.

React 19 and its history

This tutorial teaches React 19, the current major version, and uses only modern style: function components and hooks. You may meet older code that uses classes (class Foo extends React.Component); it still works, but you will not need to write it, and we only use a class once, for Error Boundaries, because React has no function-based way to do that job yet.

Version Year What it is known for
0.3 2013 First public release at JSConf US
15 2016 Cleaner DOM output, improved warnings
16 2017 A rewritten core (Fiber), error boundaries, fragments and portals
16.8 2019 Hooks: function components can hold state
17 2020 A "no new features" release that eased gradual upgrades
18 2022 createRoot, automatic batching, transitions and Suspense on the server
19 2024 Actions, use, form hooks, ref as a prop, document metadata
19.2 2025 <Activity>, useEffectEvent, performance tracks

You do not need to memorize this. The practical points are: hooks arrived in 16.8, createRoot is the 18+ way to start an app, and React 19 simplified several patterns that used to need boilerplate. When a lesson uses a feature from 19, we say so.

What you need to follow along

  • HTML and CSS basics. You should know what elements, attributes and classes are. The HTML tutorial covers this.
  • Core JavaScript. Variables, functions, arrays, objects, and ideally arrow functions, destructuring and modules. Lesson 6 of this chapter, The JavaScript You Need for React, is a focused refresher.
  • Nothing to install. Every example in this tutorial runs in the browser editor. When we reach project setup, the commands are shown as read-only text, so you can try them later on your own machine.

The live editors compile your code in the browser with a tool called Babel and load React from a CDN the first time you run something. That is perfect for learning, but it is not how you ship a real app. A real project uses a build tool that compiles ahead of time, as described in Setting Up a React Project.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does the formula UI = f(state) mean in React?

    A component is a function from data (props and state) to a description of the UI. When state changes, React calls it again and updates the DOM to match the new description.
  2. 2. Why must a component name start with a capital letter?

    JSX treats lowercase names as built-in elements (a string like "div") and capitalized names as references to your own components.
  3. 3. What happens when you call the function returned by useState, for example setCount(count + 1)?

    Setting state schedules a new render. Your component runs again with the new value, and React updates only the parts of the DOM that changed.
  4. 4. Which statement about React is true?

    React is a library focused on the view layer. The same core works with different renderers (the DOM, React Native), and since 16.8 function components can hold state with hooks.
  5. 5. Which line connects React to an empty element in the page?

    createRoot takes a DOM element and gives React ownership of it; render draws the component tree inside that root.

Summary

  • React is a library for building user interfaces out of components, which are functions that return JSX.
  • The core idea is UI = f(state): describe the screen for the current data and let React update the DOM when data changes.
  • createRoot(...).render(...) attaches a React app to an element in the page.
  • Props pass data into a component; state (useState) lets a component remember data between renders.
  • Compared with plain DOM code, React keeps the relationship between data and screen in one place, which pays off as apps grow.
  • This tutorial teaches React 19 with function components and hooks, and every example runs in your browser.

Next: How React Works