React · Getting Started

Components

A React app is a tree of components. Learning to split a screen into the right pieces is the single most important React skill.

Beginner 5 min Lesson 5 of 42Examples target React 19 with Vite.
Before you start:JSX
0/42 in React
On this page · 5 min · Beginner

Learning objectives#

  • Create and use components
  • Compose components into a tree
  • Split components into files with import/export
  • Decide when to extract a new component

Explanation#

A component is a JavaScript function whose name starts with a capital letter and that returns JSX. Use it like a tag: <Header />.

Components compose: App renders Header, ProductList and Footer; ProductList renders many ProductCard components. Data flows down this tree.

Put each component in its own file (ProductCard.jsx), export it, and import it where needed.

Extract a component when a piece of UI is repeated, when a section has its own purpose (a search bar), or when a component becomes too long to read.

Components must be pure during rendering: given the same inputs they return the same JSX and do not change outside variables.

Syntax#

JSX Runnable
function Header() { return <header>Shop</header>; }
function Footer() { return <footer>© 2026</footer>; }


export default function App() {
  return (
    <>
      <Header />
      <main>…</main>
      <Footer />
    </>
  );
}

Code example: A page built from small components#

JSXApp.jsx Runnable
function Header() {
  return (
    <header>
      <h1>Asha’s Stationery</h1>
      <nav>Home · Shop · Contact</nav>
    </header>
  );
}


function ProductCard({ name, price }) {
  return (
    <li>
      <strong>{name}</strong> — ₹{price}
    </li>
  );
}


function ProductList() {
  const products = [
    { id: 1, name: 'Pen', price: 20 },
    { id: 2, name: 'Notebook', price: 60 },
    { id: 3, name: 'Bag', price: 650 },
  ];
  return (
    <ul>
      {products.map(p => <ProductCard key={p.id} name={p.name} price={p.price} />)}
    </ul>
  );
}


function Footer() {
  return <footer>Free delivery over ₹500</footer>;
}


export default function App() {
  return (
    <>
      <Header />
      <main>
        <ProductList />
      </main>
      <Footer />
    </>
  );
}
Expected output
The page shows a header with the shop name and navigation, a list of three products (“Pen — ₹20”, “Notebook — ₹60”, “Bag — ₹650”) and a footer. The component tree is App → Header, ProductList → 3× ProductCard, Footer.

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. Header, ProductList and Footer each own one part of the page.
  2. ProductCard is reused three times with different props.
  3. ProductList turns data into ProductCard elements with map.
  4. App only arranges the pieces — easy to read at a glance.

Real-world use case#

A dashboard is split into Sidebar, TopBar, StatsCards and RecentOrdersTable. Different developers work on different components without stepping on each other.

Common mistakes#

Defining components inside other components

A component declared inside App is re-created on every render, which resets its state. Declare components at the top level.

One giant component

A 500-line App is hard to change. Extract pieces with clear names.

Calling components as functions

{ProductCard({ name })} skips React’s component handling (hooks break). Use <ProductCard name={name} />.

Best practices#

  • One component per file, named after the file
  • Small, focused components
  • Top-level component declarations only
  • Pure rendering — no side effects in the body

Remember this#

Capital letter + returns JSX = component.

App is a tree; data flows down.

Repeated or self-contained UI → its own component.

Practice exercise#

Your task: Split this into components: a page with a <SearchBar /> (an input and a button) and a <ResultsCount count={3} /> that shows "3 results".

Need a hint?
Write two small functions and use them inside a Page component.
Show solution
JSX Runnable
function SearchBar() {
  return (
    <form>
      <input placeholder="Search products" aria-label="Search products" />
      <button>Search</button>
    </form>
  );
}


function ResultsCount({ count }) {
  return <p>{count} results</p>;
}


export default function Page() {
  return (
    <>
      <SearchBar />
      <ResultsCount count={3} />
    </>
  );
}

Mini quiz#

Components quizMultiple choice Question 1 of 3

Why must component names start with a capital letter?

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.