← Learning Hub
Beginner

Components

A component is a JavaScript function that returns JSX. Its name must start with a capital letter.

Look at any website and you can split it into pieces: a header, a search bar, a product card repeated 20 times, a footer. In React each piece is a component - a function that returns what that piece looks like. Components can contain other components, so a whole page is a tree: App contains Header and ProductList, which contains many ProductCards.

The capital letter rule exists because JSX uses it to tell your components (<Welcome />) apart from normal HTML tags (<div>). In real projects each component usually lives in its own file, like ProductCard.jsx.

Example
function Welcome() {
  return <h2>Welcome!</h2>;
}

Use a component like an HTML tag: <Welcome />. You can use it as many times as you like.

Example
<!DOCTYPE html>
<html>
  <body>
    <div id="root"></div>

    <script src="https://unpkg.com/react@18.3.1/umd/react.development.js"></script>
    <script src="https://unpkg.com/react-dom@18.3.1/umd/react-dom.development.js"></script>
    <script src="https://unpkg.com/@babel/standalone@7.24.7/babel.min.js"></script>

    <script type="text/babel">
      function Logo() {
        return <h1>🛒 Terixto</h1>;
      }

      function Footer() {
        return <p>Made with React</p>;
      }

      function App() {
        return (
          <div>
            <Logo />
            <p>Compare prices before you buy.</p>
            <Footer />
          </div>
        );
      }

      ReactDOM.createRoot(document.getElementById("root")).render(<App />);
    </script>
  </body>
</html>
▶ Try it yourself
Small components are easier to read, test and reuse. If a part of the page appears in many places, make it a component.
Key points
  • A component is a function that returns JSX.
  • Component names start with a capital letter.
  • Use components like tags and nest them to build pages.

📝 Test yourself

1. What does a React component return?
2. Which is a correct component name?
3. Fill in to use the Header component:
< />