Tech News, Tech & Coding · Free Online Tools

IT Question Answer
Back to What Is
What is React and How Does it Work?

What is React and How Does it Work?

What IsBy Muhammad Fareed
react

Short answer: React is a free, open-source JavaScript library from Meta for building user interfaces. You describe the UI as reusable components, and React updates only the parts of the page that need to change when your data changes.

Components

A component is a function that returns what should appear on screen. Components can be combined like building blocks:

function Greeting({ name }) {
  return <h1>Hello, {name}!</h1>;
}

function App() {
  return (
    <div>
      <Greeting name="Asha" />
      <Greeting name="Ravi" />
    </div>
  );
}

JSX

The HTML-like syntax above is JSX. It is not HTML; a build tool converts it to JavaScript function calls. In JSX you write className instead of class, wrap JavaScript expressions in curly braces {}, and every element must be closed.

Props and state

  • Props are inputs passed from a parent to a child. They are read-only.
  • State is data a component owns and can change over time. When state changes, React re-renders the component.
import { useState } from "react";

function Likes() {
  const [likes, setLikes] = useState(0);
  return <button onClick={() => setLikes(likes + 1)}>Likes: {likes}</button>;
}

Learn the details in useState and useEffect and the complete hooks guide.

How React updates the page

  1. Your component runs and returns a description of the UI (a tree of elements).
  2. When state or props change, React runs the component again to get a new description.
  3. React compares the new description with the previous one (reconciliation, often called the virtual DOM diff).
  4. It applies only the necessary changes to the real DOM.

You write what the UI should look like for a given state; React works out how to update it. This declarative style avoids manual DOM manipulation.

Lists and conditions

function TodoList({ todos }) {
  if (todos.length === 0) return <p>Nothing to do.</p>;
  return (
    <ul>
      {todos.map((todo) => (
        <li key={todo.id}>{todo.title}</li>
      ))}
    </ul>
  );
}

Give every list item a stable key so React can track items correctly.

Starting a project

npm create vite@latest my-app -- --template react
cd my-app
npm install
npm run dev

For full-stack features such as routing and server rendering, many teams use a framework like Next.js; see what Next.js is.

Why teams choose React

  • Reusable components keep large UIs organized.
  • Large ecosystem of libraries, tools and learning material.
  • Works across platforms: web, and mobile through React Native.
  • Strong hiring market and community support.

Things to watch

  • React is a UI library, so you choose your own routing, data fetching and state tools.
  • Unnecessary re-renders can slow large apps; see performance optimization techniques.
  • Sharing state between distant components needs a plan: Context API or a state library.

FAQs

Is React a framework or a library?

The React team calls it a library focused on the view layer. Frameworks like Next.js build on it.

Do I need to know JavaScript first?

Yes. Functions, arrays, objects, destructuring and modules are used constantly.

What is the virtual DOM?

A lightweight in-memory description of the UI that React compares between renders to minimize real DOM updates.

React vs Angular vs Vue?

All are popular choices. React is the most widely used and is the most flexible; Angular is a full framework; Vue is often seen as approachable.