Himanshu SharmaSoftware engineer
Let’s talk

React · Lesson 6 of 38

Your first component

Write a React function component, learn why its name starts with a capital letter, use it like an HTML tag, reuse it several times and put it in its own file.

12 minComponents and propsUpdated 24 October 2026

By the end of this lessonWrite, name, export, import and reuse a function component, and explain why components start with a capital letter.

A component is the basic unit of a React app: a piece of the interface that has its own markup and its own behaviour. You have already written one, App, in earlier lessons. This lesson explains exactly what makes a function a component, how to use it, and how to move it into its own file.

A component is a function that returns JSX

Here is the smallest useful component:

jsx
function Welcome() {
  return <h1>Welcome to my site</h1>;
}

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

export default App;
result
Welcome to my site

Welcome is a component: an ordinary JavaScript function that returns JSX. App uses it by writing <Welcome /> as if it were an HTML tag. When React reaches that tag, it calls the Welcome function and puts whatever it returns into the page. Three rules are in play:

  1. The name starts with a capital letter. This is a requirement, not a style choice.
  2. It returns JSX (or null, to render nothing).
  3. You use it as a tag: <Welcome />, never as Welcome(). React must call the function itself, so that it can attach state and manage updates.

Why the capital letter matters

JSX treats lower-case tags as built-in HTML elements and capitalised tags as references to a function or class in scope. Try this on purpose:

jsx
function welcome() {
  return <h1>Welcome to my site</h1>;
}

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

export default App;
result
CONSOLE
error: The tag <welcome> is unrecognized in this browser. If you meant to render a React component, start its name with an uppercase letter.

The page stays empty, and the console shows a warning that <welcome /> is an unrecognised tag, because React looked for an HTML element called welcome. (The editor's Console panel shows this warning under the result.) Rename the function to Welcome and it works. Use PascalCase for components (ProductCard, NavBar).

Reusing a component

The point of components is reuse. Write once, use many times:

jsx
function Badge() {
  return <span style={{ background: "#ffd45c", padding: "2px 8px", borderRadius: 10 }}>New</span>;
}

function App() {
  return (
    <ul>
      <li>React course <Badge /></li>
      <li>PHP course</li>
      <li>jQuery course <Badge /></li>
      <li>MySQL course <Badge /></li>
    </ul>
  );
}

export default App;
result
React course New
PHP course
jQuery course New
MySQL course New

Each <Badge /> is a separate instance. They run the same code, but each has its own state (once you add state in a later lesson). If you change the colour in Badge, every badge on the page changes: one edit, many results. This is the practical benefit that makes components worth the effort.

Components return one thing

A component can contain a lot, but it returns a single root: one element, or a fragment <>...</>. It can also return null, false or nothing visible:

jsx
function Hidden() {
  return null;          // renders nothing
}

function App() {
  return (
    <>
      <p>Before</p>
      <Hidden />
      <p>After</p>
    </>
  );
}

export default App;
result
Before
After

Where components live: files and exports

In a real project, each component usually gets its own file, named after it. src/Welcome.jsx:

jsx
export default function Welcome() {
  return <h1>Welcome to my site</h1>;
}

and src/App.jsx imports it:

jsx
import Welcome from "./Welcome.jsx";

export default function App() {
  return <Welcome />;
}

Two kinds of export exist, and you will see both:

Style In the component file In the importing file
Default export (one per file) export default function Welcome() {} import Welcome from "./Welcome.jsx"
Named export (many per file) export function Welcome() {} import { Welcome } from "./Welcome.jsx"

This course uses default exports for the main component of a file. Pick one style and keep it throughout a project. The import path must start with ./ or ../ for your own files, and must match the file name, including capitals. (In the live editor, everything is one file, so you write all the components together and only the last one, App, is exported.)

Never define a component inside another

This is a very common mistake, and it works at first, which makes it harder to spot:

jsx
// Wrong: Inner is created again on every render of Outer
function Outer() {
  function Inner() {
    return <p>Hello</p>;
  }
  return <Inner />;
}

Every time Outer runs, it creates a brand new Inner function. React sees a different component type each time, throws away the old one with all its state, and builds it again. The result is lost input text, restarted animations and slow pages. Define components at the top level of the file, side by side:

jsx
function Inner() {
  return <p>Hello</p>;
}

function Outer() {
  return <Inner />;
}

It is fine to define small variables or helper functions inside a component; only component functions used as tags must be at the top level.

Naming and size

Name a component after what it shows, not where it is used: ProductCard, not HomePageBox1. When a component grows long, or you find yourself copying chunks of JSX, that is a sign to extract a smaller component. The next lesson shows how to split a page into components and combine them. A good first guideline: if you can name a piece of the screen, it can be a component.

Common mistakes

  • Starting a component name with a lower-case letter.
  • Calling a component like a function ({Welcome()}) instead of using it as a tag (<Welcome />).
  • Forgetting to return anything (a function body with no return renders nothing and logs a warning).
  • Defining components inside other components.
  • Forgetting export default, then getting "does not provide an export named default" when importing.
  • Mismatched file name case: ./welcome.jsx is not ./Welcome.jsx on a case-sensitive server, although it may work on your computer.

Exercise

  1. Make a Greeting component that returns an h2 saying "Hello!" and use it three times inside App.
  2. Change the Badge text to "Hot" in one place and watch all instances change.
  3. Rename Welcome to welcome and read the warning in the Console. Fix it again.
  4. Create a component that returns null. Why might you write one like that on purpose? (Hint: think of conditional rendering, coming in a later lesson.)

Key points

  • A component is a JavaScript function that returns JSX; use it in other JSX as a tag with the same name.
  • Component names must start with a capital letter, otherwise React treats the tag as a plain HTML element.
  • Define components at the top level of a file, never inside another component, and export them so other files can import them.
  • Each use of a component creates a separate instance, so one component can be reused as often as you like.

Check yourself

1. Why must a component name start with a capital letter?
2. What does a component return?
3. Where should you define a component?
4. You use <Badge /> three times on a page. How many instances are there?