Skip to content

What is React?

Updated Nov 30, 20264 min readBased on official documentation
What is React?

In short

React is a JavaScript library for building user interfaces out of small, reusable pieces called components. You describe what the screen should look like, and React updates it when your data changes. It is widely used to build websites and web apps.

React is a JavaScript library for building user interfaces. A user interface is everything a person sees and clicks on a web page: buttons, forms, menus and lists. React helps you build these screens from small pieces that you can reuse. The official documentation at react.dev/learn gives a good overview, and this page walks through the main ideas you need before writing your first app.

How it works

React apps are made of components. A component is a piece of the screen that has its own logic and appearance. A component can be as small as a button or as large as a full page. In practice, a React component is a plain JavaScript function that returns markup describing what should appear on the screen.

function MyButton() {
  return <button>I'm a button</button>;
}

export default function MyApp() {
  return (
    <div>
      <h1>Welcome to my app</h1>
      <MyButton />
    </div>
  );
}

Once you declare a component, you can place it inside another component, just like an HTML tag. Notice that the component name starts with a capital letter. This is how React tells your component apart from a regular HTML tag such as <button>, which always stays lowercase.

The markup in this example uses JSX. JSX is a syntax that lets you write HTML-like code inside JavaScript. It is optional, but most React projects use it because it is easy to read. JSX is stricter than HTML in a few ways. Tags such as <br /> must be closed, and a component can only return one top-level element. If you need several elements, wrap them in a shared parent, such as a <div> or an empty <> wrapper.

Showing data and making decisions

React lets you place values from your code directly into the markup. You do this with curly braces. Anything inside the braces is read as JavaScript, so {user.name} shows the value of the name property. The same idea works for attributes. Instead of quotes, you pass a variable with curly braces, for example src={user.imageUrl}.

React does not add its own syntax for conditions. You use normal JavaScript. An if statement can choose which content to build before you return it. Inside the markup, the ? operator works well for choosing between two options, and && works when you only need to show something in one case. Lists are handled with the JavaScript map() function, which turns an array of data into an array of elements. Each item in a list needs a key attribute with a unique value, usually an ID from your data. React uses these keys to track which items changed.

Responding to events and keeping state

To react to a click, you write a function and pass it to an event attribute such as onClick. Pass the function itself, without parentheses. React calls it when the event happens.

Sometimes a component must remember a value and show it, such as a counter. React handles this with state, using the built-in useState function. Calling useState(0) gives you the current value and a function to change it. When you call that function, React runs the component again so the screen shows the new value. Each copy of a component keeps its own state, so two buttons with their own counters do not affect each other.

Functions whose names start with use are called Hooks. React has built-in Hooks, and you can write your own by combining them. Hooks must be called at the top level of a component, not inside a condition or a loop.

When several components need the same value, move the state up to the closest parent that contains them. The parent then passes the value and the update function down through props, which are values given to a child component in the same way as HTML attributes.

Common mistakes

Beginners often make a few predictable mistakes. The first is calling the event handler by accident, for example writing onClick={handleClick()}. This runs the function during rendering instead of on click. The second is forgetting the key attribute in a list, which produces warnings and can cause odd updates when items move. The third is writing lowercase names for your own components, which makes React treat them as unknown HTML tags. The fourth is calling a Hook inside an if statement, which breaks the rules of Hooks.

If you already know how the page structure works, our article on What is the DOM? explains what React updates behind the scenes.

Alternatives

React is not the only option for building interfaces. Vue.js uses a different approach with templates and its own reactivity system. You can read more in our What is Vue.js? page. Choose the tool that fits your team and project. The core ideas of components, state and rendering appear in most modern frameworks, so skills you learn with React transfer well.

To start, install a recommended development tool, create a small component, and change its state with a button. Once that feels comfortable, you will be ready to learn more about sharing data between components and rendering lists.

FAQ

Is React a framework or a library?

React is a library for building user interfaces. It focuses on the view layer, so you often add other tools for routing or data fetching. Some frameworks are built on top of React and include those extras.

Do I have to use JSX with React?

No, JSX is optional. Most React projects use it because the markup is easier to read, and the common development tools support it out of the box.

Why does my component name need a capital letter?

React uses the capital letter to tell your component apart from a built-in HTML tag. Names such as button stay lowercase for HTML, while MyButton is read as a React component.

What is the difference between state and a normal variable?

A normal variable is reset every time the component runs. State, created with useState, is kept between renders, and changing it tells React to update the screen.

Sources

This page is based on the official documentation below. Read it for full detail.

React — official documentation ↗

React in the news

blozhikDeveloper news from across the web, explained. Every story links to its original sources.