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.
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.
this, no
render() method. Just a function with a return
statement. Fewer lines = fewer bugs.
useState, useEffect, useContext. This unlocks all of React's modern power.
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:
// 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:
return — runs every time the component renders.
<></> Fragment
if you need multiple elements.export default to make the
component available in other files, or export for
named exports.( ) 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.
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 Button() { return ( <button> Click Me </button> ); } export default Button;
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:
// 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>
);
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.
// 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"
/>
</>
);
}
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.
function Badge(props) { return ( <span> {props.label}: {props.count} </span> ); }
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:
// 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.
// 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
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.
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:
/* ── 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 |
8. Best Practices
Following these conventions makes your components easy for teammates (and future you) to read, test, and maintain:
UserCard.jsx exports UserCard. Easy to find, easy to replace.{'{ name, role }'} instead
of props. It makes the component's expected inputs
immediately obvious at a glance.fetch() calls, no DOM mutations, no side effects
directly in the body — put those in useEffect.
.map() over an array
to render components, always add a unique key prop.
React uses it to track items efficiently.// ❌ 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>
);
}
Explore More Tutorials
null to render nothing).
That's the complete minimum requirement.
No class, no this, no render() method needed.
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.
function Button({ label = "Click Me", color = "blue" }) { ... }
If the parent doesn't pass those props, the defaults are used automatically.
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.
props.children contain?
import Btn from './Button' but used {'{'}Btn{'}'}. What happens?fetch()
directly in the component body (outside useEffect)?- 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
childrenprop 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
keyon list items.
<ProfileCard />
that accepts name,
role,
and bio
props with default values. Render three instances with different data in App.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions, and stay updated with new tutorials.