React Tutorial menu

React Components

Components are the building blocks of a React app: each one is a small, reusable piece of the user interface.

Creating a Component

A function component is a JavaScript function that returns JSX.

Example

jsx
function Header() {
  return (
    <header>
      <h1>Karachi Computer Mart</h1>
      <p>Laptops, phones and accessories</p>
    </header>
  );
}

Two rules matter:

  • The name must start with a capital letter: Header, not header. React treats lowercase tags as plain HTML elements.
  • It must return something: JSX, null (show nothing), a string or a number.

You can also write components as arrow functions:

Example

jsx
const Footer = () => {
  return <footer>© 2026 Karachi Computer Mart</footer>;
};

Both styles work the same way. Pick one and stay consistent.

Using a Component

Use a component like an HTML tag. You can place it inside other components.

Example

jsx
function App() {
  return (
    <div>
      <Header />
      <p>Today's offer: 10% off on all keyboards.</p>
      <Footer />
    </div>
  );
}
Output
Karachi Computer Mart
Laptops, phones and accessories
Today's offer: 10% off on all keyboards.
© 2026 Karachi Computer Mart

This is called composition: big components are built from smaller ones.

A Component Tree

Every React app forms a tree. App is at the top, and other components sit inside it.

Example

text
App
├── Header
├── ProductList
│   ├── ProductCard
│   ├── ProductCard
│   └── ProductCard
└── Footer

When you plan an app, draw boxes around parts of the design. Each box that repeats or has its own job is a good component.

Components in Separate Files

In real projects each component usually has its own file. Create src/components/Header.jsx:

Example

jsx
function Header() {
  return (
    <header>
      <h1>Karachi Computer Mart</h1>
    </header>
  );
}

export default Header;

Then import it in App.jsx:

Example

jsx
import Header from "./components/Header.jsx";

function App() {
  return (
    <div>
      <Header />
      <p>Welcome!</p>
    </div>
  );
}

export default App;
Export styleHow to write itHow to import it
Default exportexport default Header;import Header from "./Header.jsx";
Named exportexport function Header() {...}import { Header } from "./Header.jsx";

A file can have only one default export, but many named exports.

Do Not Define Components Inside Components

Always define components at the top level of a file, not inside another component function. A component defined inside another is created again on every render, which causes bugs and slow pages.

Example

jsx
// Wrong
function App() {
  function Button() {
    return <button>Buy</button>;
  }
  return <Button />;
}

// Right
function Button() {
  return <button>Buy</button>;
}

function App() {
  return <Button />;
}

Keep Components Pure

A component should only calculate and return JSX. Given the same input, it should return the same output. Do not change outside variables while rendering. Things like fetching data or timers go in event handlers or in useEffect, which you will learn later.

A good habit: if a component file grows beyond about 150 lines, look for a part you can move into its own component.

Exercise

Exercise

Why must a React component name start with a capital letter?

Learn React with a teacher

Join the Full Stack Development course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp