Tutorials React JS Functional Components
Ch 7 / 49
Chapter 7 Beginner 10 min read React JS

Functional Components

A Functional Component is the simplest and most common way to build UI in React. It is just a JavaScript function that returns JSX. In this chapter you will learn exactly how to write one, how to pass data in using props, the difference between function declarations and arrow functions, how to export components, and the best practices every React developer follows day-to-day. No classes, no lifecycle methods — just clean, simple functions.

Advertisement
What you'll learn
What a functional component is — the simplest definition
Function declaration syntax — the classic way to define a component
Arrow function syntax — the modern shorthand style
Accepting props — passing data into a component from outside
Destructuring props — cleaner, more readable syntax
Default prop values — fallback values when no prop is passed
Named vs default exports — how to share components between files
Best practices — naming, file structure, single responsibility

1. What is a Functional Component?

A Functional Component is a plain JavaScript function whose name starts with a capital letter and returns JSX. That's it. No classes, no this, no special setup.

React calls your function, takes the JSX it returns, and turns that into real DOM elements you see on screen. Every time something changes (state or props), React calls your function again and updates only what's different.

One-line definition A functional component = a JavaScript function that returns JSX. React sees the capital letter, calls the function, and renders what it returns.
Shorter Code
No constructor, no this, no render() method. Just a function with a return statement. Fewer lines = fewer bugs.
Simplicity
Hooks Support
Only functional components can use React Hooks like useState, useEffect, useContext. This unlocks all of React's modern power.
Modern React
Easy to Test
A functional component is a pure function — you call it with props and check the output. No class instantiation, no lifecycle mocking needed.
Testability
React Team Recommends
Since React 16.8, the official recommendation is to use functional components for all new code. Class components still work but are considered legacy.
Official

2. Basic Syntax — Function Declaration

The most straightforward way to write a functional component is using a function declaration. You write function, give it a PascalCase name, and return JSX:

jsx — Greeting.jsx — simplest functional component
// Step 1: Define the component — just a function
function Greeting() {
  return (
    <div>
      <h1>Hello, World!</h1>
      <p>Welcome to React.</p>
    </div>
  );
}

// Step 2: Use it like an HTML tag (capital letter!)
function App() {
  return <Greeting />;
}

// Step 3: Export so other files can import it
export default App;

Every functional component has the same 3-part structure:

1
Function Name
Must be PascalCase — first letter capital. This is how React knows it's a component, not an HTML tag.
e.g. Greeting
2
Optional Logic
Variables, calculations, event handlers. Everything above the return — runs every time the component renders.
JS code here
3
Return JSX
The UI description. Must return a single root element. Use <></> Fragment if you need multiple elements.
return (JSX)
4
Export
Use export default to make the component available in other files, or export for named exports.
export default
Parentheses around JSX Wrapping multi-line JSX in ( ) is optional but strongly recommended. It prevents JavaScript's automatic semicolon insertion from silently breaking your return statement if the opening tag is on the next line.
Advertisement

3. Arrow Function Style

You can also write a functional component using an arrow function. Both styles produce exactly the same result — it's purely a matter of preference. Most modern React codebases use arrow functions because they are slightly shorter.

Function Declaration
function Button() {
  return (
    <button>
      Click Me
    </button>
  );
}

export default Button;
Arrow Function
const Button = () => {
  return (
    <button>
      Click Me
    </button>
  );
};

export default Button;

If your component returns only JSX and has no logic above the return, you can use an implicit return — remove the curly braces and wrap the JSX in parentheses:

jsx — implicit return (arrow function shorthand)
// Normal arrow with explicit return
const Tag = () => {
  return <span>React</span>;
};

// Implicit return — same result, no return keyword needed
const Tag = () => <span>React</span>;

// Implicit return with multi-line JSX — wrap in ( )
const Card = () => (
  <div className="card">
    <h2>Title</h2>
    <p>Description</p>
  </div>
);
Hoisting difference Function declarations are hoisted — you can use them before they are defined in the file. Arrow functions stored in const are not hoisted — you must define them before using them. In practice, put all component definitions at the top of the file to avoid confusion.

4. Using Props — Passing Data into Components

Props (short for properties) are how you pass data into a component from its parent. Think of them as the component's input — like passing arguments to a function. React automatically collects all the attributes you put on a component tag and passes them as a single props object to your function.

jsx — UserCard.jsx — receiving and using props
// React passes ALL attributes as one "props" object
function UserCard(props) {
  return (
    <div className="user-card">
      <img src={props.avatar} alt={props.name} />
      <h2>{props.name}</h2>
      <p>{props.role}</p>
    </div>
  );
}

// Use it — pass data like HTML attributes
function App() {
  return (
    <>
      <UserCard
        name="Priya Sharma"
        role="Lead Developer"
        avatar="/images/priya.jpg"
      />
      <UserCard
        name="Rahul Mehta"
        role="React Engineer"
        avatar="/images/rahul.jpg"
      />
    </>
  );
}
Props are read-only A component must never modify its own props. Props flow one way — from parent to child. If you need to change something, use useState (covered in Chapter 9 — State).
What you write on the tag What arrives in props Type
name="Priya" props.name → "Priya" string
age={25} props.age → 25 number
active={true} props.active → true boolean
active (no value) props.active → true boolean shorthand
items={[1,2,3]} props.items → [1,2,3] array
onClick={handleClick} props.onClick → function function
children (nested JSX) props.children → JSX ReactNode

5. Destructuring Props — Cleaner Syntax

Writing props.name, props.role everywhere gets repetitive. The standard React pattern is to destructure the props object directly in the function parameter. The result is exactly the same — just cleaner to read and write.

Without destructuring
function Badge(props) {
  return (
    <span>
      {props.label}: {props.count}
    </span>
  );
}
With destructuring ✅
function Badge({ label, count }) {
  return (
    <span>
      {label}: {count}
    </span>
  );
}

There is one special prop — children. It contains whatever JSX you put between the component's opening and closing tags:

jsx — the children prop — building wrapper/layout components
// A wrapper component that uses props.children
function Card({ title, children }) {
  return (
    <div className="card">
      <h3>{title}</h3>
      <div className="card-body">
        {children}   {/* whatever is nested inside <Card>...</Card> */}
      </div>
    </div>
  );
}

// Using it — anything between the tags becomes "children"
function App() {
  return (
    <Card title="User Info">
      <p>Name: Priya Sharma</p>
      <p>Role: Developer</p>
    </Card>
  );
}

6. Default Prop Values

Sometimes a parent might not pass a prop. Instead of crashing or showing undefined, you can define default values right inside the destructuring parameter. This is the modern React pattern — no need for defaultProps.

jsx — default values via destructuring
// Default values assigned with = inside destructuring
function Button({
  label = "Click Me",       // fallback if parent doesn't pass label
  color = "blue",         // fallback if parent doesn't pass color
  disabled = false,     // fallback boolean
  onClick = () => {},    // fallback no-op function (safe to call)
}) {
  return (
    <button
      style={{ background: color }}
      disabled={disabled}
      onClick={onClick}
    >
      {label}
    </button>
  );
}

// Caller passes nothing — defaults kick in
<Button />                              // "Click Me", blue, enabled

// Caller overrides specific props
<Button label="Submit" color="green" />  // "Submit", green, enabled
Why a no-op default for functions? Setting onClick = () => {} as the default means you can always safely call onClick() inside the component without checking if it's defined. Calling undefined() would throw a TypeError.
⭐⭐⭐⭐⭐
4.9 / 5
🔥 Most Popular at K2infocom · Vadodara
React JS Complete
Masterclass
From functional components all the way to full-stack React apps. Hooks, State, Context, Router, Redux, REST APIs & 6 real-world projects — guided by experts.
Components Hooks & State React Router Redux Toolkit REST APIs 6 Projects Certificate
2,500+Students
6+Projects
3–6Months
100%Placement

7. Exporting Components — Named vs Default

To use a component in another file you must export it. There are two kinds of exports in JavaScript and React uses both:

jsx — default vs named exports and how to import them
/* ── DEFAULT EXPORT ─────────────────────────────────────── */
// Button.jsx
function Button({ label }) {
  return <button>{label}</button>;
}
export default Button;

// Import — name can be ANYTHING (convention: same name)
import Button from "./Button";
import MyBtn  from "./Button";  // also valid (different alias)

/* ── NAMED EXPORT ────────────────────────────────────────── */
// icons.jsx — multiple exports from one file
export function IconHome()  { return <span>🏠</span>; }
export function IconUser()  { return <span>👤</span>; }
export function IconBell()  { return <span>🔔</span>; }

// Import — name MUST match exactly (use curly braces)
import { IconHome, IconUser } from "./icons";
Feature Default Export Named Export
Syntax export default MyComp export function MyComp
Per-file limit 1 per file Unlimited
Import syntax import X from './file' import { X } from './file'
Import name freedom Any name works Must match export name
Best used for The main component of a file Utilities, multiple components
Advertisement

8. Best Practices

Following these conventions makes your components easy for teammates (and future you) to read, test, and maintain:

One Component Per File
Put each component in its own file. Name the file the same as the component: UserCard.jsx exports UserCard. Easy to find, easy to replace.
Structure
Keep Components Small
If your component is over ~80 lines, split it. A component should do one thing. Extract sub-parts into their own components.
Single Responsibility
Always Destructure Props
Use {'{ name, role }'} instead of props. It makes the component's expected inputs immediately obvious at a glance.
Readability
Never Define Inside a Component
Don't define a component inside another component's function body. It creates a brand new function every render, destroying all state. Always define at the module level.
Correctness
Keep Render Pure
The function body should only read props/state and compute JSX. No fetch() calls, no DOM mutations, no side effects directly in the body — put those in useEffect.
Purity
Add key When Rendering Lists
When you .map() over an array to render components, always add a unique key prop. React uses it to track items efficiently.
Lists
jsx — best practice patterns side-by-side
// ❌ Bad — component defined inside another component
function ProductList({ products }) {
  function ProductItem({ name }) {  // ❌ new function every render!
    return <li>{name}</li>;
  }
  return <ul>{products.map(p => <ProductItem name={p.name} />)}</ul>;
}

// ✅ Good — defined at module level + key prop on list items
function ProductItem({ name }) {   // ✅ same reference every render
  return <li>{name}</li>;
}

function ProductList({ products }) {
  return (
    <ul>
      {products.map(p => (
        <ProductItem key={p.id} name={p.name} />  // ✅ key added
      ))}
    </ul>
  );
}
Knowledge Check — Functional Components
Q1. What are the two things every functional component must have?
A functional component must have: (1) a PascalCase name — starting with a capital letter so React can distinguish it from native HTML elements, and (2) it must return JSX (or null to render nothing). That's the complete minimum requirement. No class, no this, no render() method needed.
Q2. What is the difference between a function declaration component and an arrow function component?
They produce identical output in React — both are valid functional components.

Function declaration (function MyComp() {}) is hoisted, so you can call it before it's defined.

Arrow function (const MyComp = () => {}) is not hoisted and must be defined before use.

Most teams pick one style and stay consistent.
Q3. How do you set a fallback value for a prop that might not be passed?
Use default values in destructuring directly in the parameter list:

function Button({ label = "Click Me", color = "blue" }) { ... }

If the parent doesn't pass those props, the defaults are used automatically.
Q4. When should you use a named export vs a default export?
Default export is usually for the main component of a file.

Named export is used when exporting multiple items from a file.

The most common pattern is one default export for the component and named exports for helpers or utilities.
Multiple Choice Questions
MCQ 1
Which of the following is a valid functional component?
MCQ 2
What does props.children contain?
MCQ 3
You try to import a default-exported component: import Btn from './Button' but used {'{'}Btn{'}'}. What happens?
MCQ 4
Why should you never call fetch() directly in the component body (outside useEffect)?
Chapter 7 Summary — Functional Components
  • A Functional Component is a JavaScript function with a PascalCase name that returns JSX — the simplest and most recommended way to build UI in React.
  • You can write components as function declarations or arrow functions — both work identically; arrow functions support implicit return for short components.
  • Props are the component's input — passed as attributes on the JSX tag, received as a single object. Props are read-only.
  • Destructuring props in the parameter ({'{ name, role }'}) is the standard pattern — cleaner and self-documenting.
  • Default values ({'{ label = "Click Me" }'}) prevent undefined errors when a parent doesn't pass a prop.
  • The special children prop holds JSX content placed between the component's opening and closing tags — useful for wrapper/layout components.
  • Default export — one per file, importers choose the name. Named export — unlimited per file, importers must match the name exactly.
  • Best practices: one component per file, keep components small, never define a component inside another, keep the render body pure (no side effects), always add key on list items.
Practice — Build a ProfileCard Component
Open a live React sandbox. Write a <ProfileCard /> that accepts name, role, and bio props with default values. Render three instances with different data in App.
Open Vite + React Sandbox

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
If this tutorial helped you, a 5-star Google review means a lot. It helps other learners discover our free content and motivates us to keep improving.
500+ Reviews (Target)
Write a Google Review
Join WhatsApp Channel