Skip to content

Components

Function components, composition, and the building blocks of every React application.

What you'll learn

  • How to define and use function components
  • Composing small components into larger UIs
  • The difference between function and class components
  • Naming conventions and file organization for components

Concept

What Is a Component?

A React component is a JavaScript function that returns JSX. It takes optional inputs called props and returns a description of what should appear on the screen. Components are the atomic unit of a React application — everything you see is built from components. A button is a component, a form is a component, a whole page is a component.

Composition Over Inheritance

React strongly favors composition over inheritance. Instead of creating deep class hierarchies, you build complex UIs by combining simple, focused components. A Card component might contain a Header, a Body, and a Footer. Each is independent, reusable, and testable. This approach scales far better than inheritance and keeps each component small and understandable.

Function vs Class Components

Historically, React supported two types of components: class components and function components. Today, function components are the standard. Class components are still supported for backwards compatibility but are rarely written in new code. Function components are simpler, have less boilerplate, and work seamlessly with Hooks (which you will learn about soon). If you see old tutorials using class MyComponent extends React.Component, know that this is legacy syntax.

Naming and Organization

Component names must start with a capital letter — React treats lowercase tags as DOM elements (<div>) and capitalized tags as components (<MyCard>). Organize components into files: one primary component per file, named the same as the file. Group related components into folders. Keep components small — if a component exceeds 200 lines, consider splitting it.

Component Reusability

The power of components lies in reusability. A well-designed component is generic enough to be used in multiple contexts but specific enough to be meaningful. Avoid hard-coding values that should be props. Use sensible default props so the component works out of the box but can be customized. The children prop is especially powerful for creating wrapper components that do not care about their content — like layout containers, cards, and panels.

Example

              // Small, focused, reusable components composed together.
function Button({ label, onClick }) {
  return <button className="btn" onClick={onClick}>{label}</button>;
}

function Card({ title, children }) {
  return (
    <div className="card">
      <h3 className="card-title">{title}</h3>
      <div className="card-body">{children}</div>
    </div>
  );
}

// Compose them into a complete UI.
function App() {
  return (
    <Card title="Welcome">
      <p>This is the card content.</p>
      <Button label="Click me" onClick={() => alert("Clicked!")} />
    </Card>
  );
}
            

Button and Card are independent, reusable components. The children prop lets Card accept any content. App composes them together — this is composition in action.

Try it

  • JSON Formatter

    Component props are often JSON objects — format and inspect them.

  • Code Diff

    Compare component versions when refactoring.

Common mistakes

The mistake

Starting component names with a lowercase letter

The fix

React treats lowercase tags as DOM elements (like <div>) and capitalized tags as components. Always capitalize component names: function MyCard(), not function myCard().

The mistake

Making components too large with too many responsibilities

The fix

Keep components small and focused. If a component does too much, split it into smaller sub-components and compose them. Aim for one responsibility per component.

Practice Components

7 exercises

Practice Components

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors