React · Getting Started

Introduction to React

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 keeps the page in sync with your data.

Beginner 5 min Lesson 1 of 42Examples target React 19 with Vite.
0/42 in React
On this page · 5 min · Beginner

Learning objectives#

  • Explain what React is and what problem it solves
  • Understand components, props and state at a high level
  • Know how React updates the page efficiently
  • Read a first React component

Explanation#

In plain JavaScript you change the page step by step: find an element, change its text, add a class. As apps grow, keeping every element in sync with the data becomes the hardest part.

React is declarative: you write a function that returns what the UI should look like for the current data. When the data changes, React works out the minimal DOM changes for you.

Everything is a component — a function that returns markup written in JSX. Components receive inputs (props), keep their own changing data (state) and are combined into bigger components.

React compares the new output with the previous one (reconciliation) and updates only what changed, so you never touch the DOM directly.

React was created at Facebook (Meta) and is used by Netflix, Airbnb, Instagram and countless companies. React itself only handles the UI; routing, data fetching and builds come from tools like React Router and Vite.

Syntax#

JSX Illustrative
function Welcome({ name }) {
  return <h1>Hello, {name}!</h1>;
}

Code example: A first interactive component#

JSXApp.jsx Runnable
import { useState } from 'react';


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


export default function App() {
  const [likes, setLikes] = useState(0);


  return (
    <main>
      <Greeting name="Asha" />
      <p>This page has {likes} likes.</p>
      <button onClick={() => setLikes(likes + 1)}>👍 Like</button>
    </main>
  );
}
Expected output
The page shows “Hello, Asha!”, “This page has 0 likes.” and a Like button. Each click increases the number; React updates only that text, not the whole page.

This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.

Step-by-step explanation#

  1. Greeting is a component: a function that receives props (name) and returns JSX.
  2. App uses Greeting like an HTML tag: <Greeting name="Asha" />.
  3. useState(0) gives App a piece of state, likes, and a function to change it.
  4. Clicking the button calls setLikes; React re-runs App and updates the changed text.

Real-world use case#

An online shop builds a ProductCard component once and reuses it hundreds of times on listing, search and wishlist pages, each time with different props.

Common mistakes#

Changing the DOM directly

document.getElementById(...).innerText = ... fights with React. Change state and let React update the DOM.

Thinking React is a full framework

React is the view layer. You add a router, data fetching and a build tool around it.

Lowercase component names

<greeting /> is treated as an HTML tag. Components must start with a capital letter.

Best practices#

  • Think in components: split the UI into small pieces
  • Keep components pure: same props → same output
  • Change data, not the DOM
  • Learn modern JavaScript first

Remember this#

UI = function(data). Change the data; React updates the screen.

Component = function that returns JSX.

Props come in; state lives inside.

Practice exercise#

Your task: Write a component ProfileCard that receives name and city props and renders them in an <article> with an <h2> and a <p>.

Need a hint?
Destructure the props in the parameter list: function ProfileCard({ name, city }).
Show solution
JSX Runnable
export default function ProfileCard({ name = 'Ravi', city = 'Pune' }) {
  return (
    <article>
      <h2>{name}</h2>
      <p>Lives in {city}</p>
    </article>
  );
}
// <ProfileCard name="Asha" city="Bengaluru" />

Mini quiz#

Introduction to React quizMultiple choice Question 1 of 3

What does “declarative” mean in React?

Score so far: 0/0

Want more? Practise all React MCQs · React interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.