Lesson

React Components

10 min reading
Free Course

React Components: Reusable Building Blocks for Your UI

React components are independent JavaScript functions that accept input data, called props, and return JSX markup describing what should appear on screen. Splitting a page into focused components lets you build, test, and reuse buttons, cards, and forms across screens without copying layout code.

This guide covers how to write and compose function components, keep them pure, organize files, and avoid the mistakes that most often cause lost state and confusing bugs.

What Are React Components?

A component is a function that takes an object of props and returns something React can display, usually JSX. You use it like an HTML tag, and React calls the function to find out what to render. Small components combine into bigger ones, which is called composition.

Components can return JSX, strings, numbers, arrays, or null. Returning null renders nothing.

Writing and Composing Function Components

Every React component name must start with an uppercase letter, such as AlertBanner or PricingTier. Lowercase tag names like <card /> tell React to render a built-in HTML tag instead of calling your JavaScript function.

function StatusPill({ active }) {
  return (
    <span className={active ? 'pill pill-green' : 'pill pill-gray'}>
      {active ? 'Active' : 'Paused'}
    </span>
  );
}

export default function ProjectRow({ title, owner, active }) {
  return (
    <div className="project-row">
      <h3>{title}</h3>
      <p>Owner: {owner}</p>
      <StatusPill active={active} />
    </div>
  );
}

ProjectRow uses StatusPill inside its markup, which is composition. Rendering <ProjectRow title="Docs Portal" owner="Nadia" active={true} /> produces:

<div class="project-row"><h3>Docs Portal</h3><p>Owner: Nadia</p><span class="pill pill-green">Active</span></div>

Why Component Names Must Be Uppercase

JSX compiles a tag by looking at its first letter. A lowercase tag such as <card /> becomes a string, so React treats it as a built-in HTML element named card. An uppercase tag such as <Card /> becomes a reference to your function, so React calls it.

In a test render, <userCard /> produced a literal <userCard> element and a React warning about incorrect casing. So the rule applies to the JSX tag name. If you want to use a component stored under a lowercase variable, assign it to a capitalized name first.

Keeping React Components Pure

A pure component returns the same JSX every time it receives the same props and state. To keep it pure:

  • Don't change variables that live outside the component.

  • Don't mutate props.

  • Don't fetch network data, write to the DOM, or set timers directly in the render body.

Put side effects in event handlers or in useEffect. React's StrictMode renders components twice in development to expose impure code, so a surprising double log or double increment is often a sign of a purity problem.

Organizing Component Files

A common convention is one main component per file inside src/components/. It isn't a React rule, and frameworks may suggest their own folder layouts. A few habits help in large projects:

  • Keep a tiny helper component in the same file only when nothing else uses it.

  • Name files after the component they export.ProjectRow.jsx).

  • Use clear, consistent export names so imports stay predictable.

  • Group related components in a folder once a feature grows.

Common Component Pitfalls

  • Defining Components Inside Other Components: Declaring function Child() inside function Parent() creates a brand-new function reference on every render. React unmounts and remounts the child each time, wiping out its state and input focus.

  • Using Lowercase Function Names: Naming a component function userCard() causes React to emit <usercard></usercard> in the DOM instead of executing your function.

Frequently Asked Questions

How small should a React component be?

Split a component whenever it handles more than one distinct UI responsibility or grows past roughly 100–150 lines of readable JSX.

Should I use function components or class components?

Always choose function components for new code. Function components support hooks, produce smaller bundles, and work with React 19 features like Server Components.

Can a component return null?

Yes. Returning null (or false) tells React to render nothing for that component while keeping the rest of the tree intact.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol
React Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum