4. What Are Components in React, and How Do They Work

🧩 React Components

What are Components?

Types of Components

  1. Functional Components – Modern standard, examples below.

  2. Class Components – Older style, less used now.

Think of components like JavaScript functions that return UI elements.

JSX (JavaScript + HTML)

Example:

function Greeting() {
const name = "John";
return <h1 className="title">Hello {name}</h1>;
}

Notes on JSX

Rendering Multiple Elements

function Greeting() {
const name = "John";
return (
<>
<h1>Hello {name}</h1>
<p>Nice to meet you.</p>
</>
);
}

Key Takeaways