TExcelTAD
Home › React › React Components

React Components

Components are the building blocks of every React app. A component is a JavaScript function that returns JSX. You can use it as many times as you like, nest it inside other components and give it its own data.

Defining and using a component

jsx · try it yourself
function Logo() {
  return <span style={{ fontSize: 28 }}>🍜 Pho House</span>;
}

function OpeningHours() {
  return <p>Open daily 7:00 – 21:00</p>;
}

export default function App() {
  return (
    <header style={{ borderBottom: '2px solid #eee', paddingBottom: 8 }}>
      <Logo />
      <OpeningHours />
    </header>
  );
}

Three rules make a function a component:

  1. Its name starts with a capital letter.
  2. It returns JSX (or null to render nothing).
  3. It is used as a tag: <Logo />, not called as Logo().

A component tree

Components form a tree. The top component (App) renders children, which render their own children, and so on:

App
├── Header
│   ├── Logo
│   └── Nav
├── Menu
│   ├── Dish
│   ├── Dish
│   └── Dish
└── Footer

Let's build it:

jsx · try it yourself
function Logo() {
  return <strong>🍜 Pho House</strong>;
}

function Nav() {
  return (
    <nav style={{ display: 'flex', gap: 12 }}>
      <span>Menu</span>
      <span>About</span>
      <span>Contact</span>
    </nav>
  );
}

function Header() {
  return (
    <header style={{ display: 'flex', justifyContent: 'space-between', padding: 8, background: '#fff4e0' }}>
      <Logo />
      <Nav />
    </header>
  );
}

function Dish() {
  return <li>Beef pho — 55,000₫</li>;
}

function Menu() {
  return (
    <section id="menu">
      <h2>Menu</h2>
      <ul>
        <Dish />
        <Dish />
        <Dish />
      </ul>
    </section>
  );
}

function Footer() {
  return <footer style={{ color: 'gray' }}>© Pho House</footer>;
}

export default function App() {
  return (
    <div>
      <Header />
      <Menu />
      <Footer />
    </div>
  );
}

All three dishes look the same — a component without inputs always renders the same thing. The next lesson introduces props so that each <Dish> can show different data.

Components in separate files

In a real project each component usually gets its own file and is exported:

jsx
// src/components/Logo.jsx
export default function Logo() {
  return <strong>🍜 Pho House</strong>;
}

// src/App.jsx
import Logo from './components/Logo.jsx';

export default function App() {
  return <Logo />;
}
Export style Syntax Import with
Default export default function Logo() import Logo from './Logo.jsx'
Named export function Logo() import { Logo } from './Logo.jsx'

Teams often use one default export per component file, with the file named after the component.

Components keep their own state

Each instance of a component has its own independent state. Click the buttons below: every counter remembers its own number.

jsx · try it yourself
import { useState } from 'react';

function Counter() {
  const [n, setN] = useState(0);
  return (
    <button onClick={() => setN(n + 1)} style={{ margin: 4, padding: '6px 14px' }}>
      {n}
    </button>
  );
}

export default function App() {
  return (
    <div>
      <p>Three independent counters:</p>
      <Counter />
      <Counter />
      <Counter />
    </div>
  );
}

Pure rendering

React expects components to behave like pure functions during rendering: given the same props and state, they return the same JSX and don't change anything outside themselves. Don't modify global variables or the DOM while rendering — do side effects in event handlers or in useEffect.

jsx
let guests = 0;

function BadCup() {
  guests = guests + 1;            // ❌ changes outside data while rendering
  return <p>Cup for guest #{guests}</p>;
}

function GoodCup({ guest }) {
  return <p>Cup for guest #{guest}</p>;  // ✅ output depends only on props
}

Tip: Never define a component inside another component. The inner component would be a brand-new function on every render, so React would throw away its state each time. Declare components at the top level of the module.

Function vs. class components

Older code defines components as classes extending React.Component. Function components with hooks are the modern standard and are used throughout this course; the Class Components lesson shows the legacy form so you can read old code.

Test yourself

How do you render a component named Profile?

  1. {Profile}
  2. <profile />
  3. <Profile />
  4. render(Profile)

Two <Counter /> elements are rendered side by side. What happens when you click the first one?

  1. Both counters increase
  2. Only the first counter increases
  3. Neither changes
  4. React throws an error

Why should you avoid defining a component inside another component?

  1. It is a syntax error
  2. Its state is reset on every render of the parent
  3. It can't receive props
  4. It can't return JSX