LearnContact
Lesson 1224 min read

Functional Components

Go deep on function components — writing them, composing them, why they became the standard, and how to export and import them across files.

Introduction

In the last lesson, you learned that React supports two ways to write components: functions and classes. Function components are where nearly all modern React code lives, so this lesson goes deep on exactly how to write them, combine them, and share them across files.

By the end of this lesson you will be comfortable writing your own function components from scratch, understanding why they became React's preferred style, and organizing components across multiple files using exports and imports — a skill you will use in literally every React project you build.

What You Will Learn
  • How to write a function component that returns JSX.
  • How to compose multiple function components together.
  • Why function components plus Hooks became the standard over classes.
  • How to export and import components across files, and the difference between default and named exports.

Writing a Function Component

A function component is simply a JavaScript function that returns JSX describing what should appear on screen. It can be written with the function keyword or as an arrow function, and by convention its name starts with a capital letter.

Two Equivalent Ways to Write the Same Component
// Using the function keyword
function Greeting() {
  return <h1>Hello there!</h1>;
}

// Using an arrow function
const Greeting = () => {
  return <h1>Hello there!</h1>;
};
Rendered on the Page
Hello there!

Just a Function

No special class syntax — it is plain JavaScript that happens to return JSX.

Accepts Props

It can receive a single "props" object as its argument, covered fully in the Props lesson.

Returns JSX

The return value describes the UI React should render for this component.

Called by React

You never call it directly — React calls it for you whenever it needs to render or re-render.

Composing Function Components

Just like in the last lesson, function components combine by being referenced inside each other's JSX. Because each one is just a function, you can build very complex UIs by composing many small, well-named function components together.

Composing Function Components
function Logo() {
  return <h2>PrograMinds</h2>;
}

function NavLinks() {
  return (
    <nav>
      <a href="/home">Home</a>
      <a href="/courses">Courses</a>
    </nav>
  );
}

function Navbar() {
  return (
    <header>
      <Logo />
      <NavLinks />
    </header>
  );
}
Rendered on the Page
PrograMinds
Home  Courses

Why Function Components Became the Standard

Early React relied heavily on class components, especially for anything that needed internal state or lifecycle behavior. That changed in 2019 when React introduced Hooks, letting function components do everything classes could — and more simply.

Function Components + Hooks

  • Simpler syntax — no class boilerplate, no constructor.
  • No "this" binding issues to worry about.
  • Logic is easier to reuse via custom Hooks.
  • Generally easier to test in isolation.

Class Components

  • More boilerplate — constructor, render(), extends React.Component.
  • this can be a source of confusing bugs if not bound correctly.
  • Reusing stateful logic between classes is awkward (often needs HOCs or render props).
  • Falling out of use in new codebases, though still common in legacy projects.
The Big Shift

Hooks let function components hold state, run side effects, and access context — all things that used to require a class. This removed nearly every reason to reach for a class component in new code.

Exporting and Importing Components

Real React projects split components across many files. To use a component defined in one file from another file, you export it from where it is defined and import it wherever you need it. JavaScript modules give you two export styles: default exports and named exports.

Button.jsx — Default Export
function Button({ text }) {
  return <button>{text}</button>;
}

export default Button;
Icons.jsx — Named Exports
export function HomeIcon() {
  return <span>🏠</span>;
}

export function SearchIcon() {
  return <span>🔍</span>;
}
App.jsx — Importing Both Styles
// Default export: choose any name, no curly braces
import Button from './Button';

// Named exports: exact name, inside curly braces
import { HomeIcon, SearchIcon } from './Icons';

function App() {
  return (
    <div>
      <HomeIcon />
      <SearchIcon />
      <Button text="Click me" />
    </div>
  );
}

export default App;
Export StyleSyntaxImport SyntaxPer File Limit
Default exportexport default Button;import Button from './Button';Only one per file
Named exportexport function HomeIcon() {}import { HomeIcon } from './Icons';As many as you like

Example: A Small Multi-File App

Putting it all together, here is how a tiny app might be organized across three files, each with one clearly named function component.

Greeting.jsx
function Greeting() {
  return <h1>Welcome back!</h1>;
}

export default Greeting;
App.jsx
import Greeting from './Greeting';

function App() {
  return (
    <div>
      <Greeting />
      <p>Your dashboard is ready.</p>
    </div>
  );
}

export default App;
Rendered on the Page
Welcome back!
Your dashboard is ready.

Common Mistakes

Avoid These Mistakes
  • Wrapping a named import in curly braces incorrectly, or forgetting curly braces are required for named imports but not for default imports.
  • Having more than one default export in a single file — only one is allowed.
  • Forgetting to actually return JSX from the function — a function component with no return statement renders nothing.
  • Mixing up the import name for a default export; since default imports can be named anything, a typo will not immediately error but will fail silently if names drift across files.

Best Practices

  • Use one component per file where practical, matching the file name to the component name.
  • Prefer default exports for the "main" component of a file, and named exports for smaller helper components.
  • Keep function components pure — given the same props, they should render the same UI.
  • Favor arrow function or function-keyword syntax consistently across your codebase.
  • Extract repeated JSX into its own function component instead of copy-pasting markup.

Frequently Asked Questions

Do function components need to be capitalized?

Yes — this follows the same naming rule as any React component. A capitalized name tells React (and JSX) that this is a component, not an HTML tag.

Can a function component contain other function declarations inside it?

It can, but you should avoid defining other components inside it. Defining a component inside another recreates it on every render, which can cause subtle bugs and hurt performance.

What happens if I forget to export a component?

Any file trying to import it will get an error (or undefined), because JavaScript modules only expose what you explicitly export.

Can I use both a default export and named exports in the same file?

Yes, a single file can have one default export alongside any number of named exports.

Do I still need to learn class components if function components are the standard?

It helps to recognize class syntax for reading legacy code and some older libraries, even though you will rarely write new class components. That is exactly what the next lesson covers.

Key Takeaways

  • A function component is a plain JavaScript function that returns JSX.
  • Function components compose together the same way any components do, by nesting.
  • Hooks gave function components state and lifecycle capabilities, removing the main reason to use classes.
  • Components are shared across files with export and import, using either default or named exports.
  • Default exports use any import name with no curly braces; named exports require the exact name inside curly braces.

Summary

Function components are the backbone of modern React development: simple functions that return JSX, composed together, and shared across files using JavaScript modules. Understanding exports and imports is essential, since virtually every real project splits its UI across many component files.

In this lesson, you learned how to write and compose function components, why they overtook class components as the standard, and how to organize them with default and named exports. Next, you will look at class components — the older syntax you will still encounter in legacy React code.

Lesson 12 Completed
  • You can write and compose function components confidently.
  • You understand why function components with Hooks became the standard.
  • You can export and import components using both default and named exports.
  • You are ready to explore class components next.
Next Lesson →

Class Components