Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 4 of 10
Functional Components & Props
Every React UI is made of components. A component is a pure function: given the same props, it always returns the same JSX. Understanding props - how to type them, pass them, default them, and compose them - is the skill you'll use in every single line of React you write.
Component Purity - The Most Important Rule
// A component MUST be pure with respect to its props:
// Same props โ same JSX output, every time, with no side effects during rendering
// โ
PURE - deterministic, no external side effects during render
function UserCard({ name, role }: { name: string; role: string }) {
return (
<div className="card">
<h3>{name}</h3>
<span>{role}</span>
</div>
);
}
// โ IMPURE - modifies an external variable during render (bug-prone)
let renderCount = 0;
function BadComponent({ name }: { name: string }) {
renderCount++; // โ side effect during render - breaks StrictMode
return <div>{name}</div>;
}
// โ IMPURE - reads from external mutable state during render
function NotPure({ id }: { id: number }) {
const user = window.currentUser; // โ external mutable read
return <div>{user.name}</div>;
}
// Side effects (API calls, subscriptions, DOM mutations) belong in useEffect - never in the render bodyTypeScript Props - The Production Way
// Define props with an interface (not React.FC - it has issues)
interface UserCardProps {
name: string;
role: 'admin' | 'user' | 'guest';
avatarUrl?: string; // optional prop
onSelect: (id: number) => void; // callback prop
children?: React.ReactNode; // anything React can render
}
// โ
Preferred: plain function with typed parameter
function UserCard({ name, role, avatarUrl, onSelect, children }: UserCardProps) {
return (
<div className="card" onClick={() => onSelect(1)}>
{avatarUrl && <img src={avatarUrl} alt={name} />}
<h3>{name}</h3>
<span className={`badge ${role}`}>{role}</span>
{children}
</div>
);
}
// Default values - use destructuring defaults, NOT defaultProps
function Button({
variant = 'primary',
size = 'md',
disabled = false,
children,
}: {
variant?: 'primary' | 'secondary' | 'ghost';
size?: 'sm' | 'md' | 'lg';
disabled?: boolean;
children: React.ReactNode;
}) {
return (
<button
className={`btn btn-${variant} btn-${size}`}
disabled={disabled}
>
{children}
</button>
);
}Component Composition - Building Complex UIs
// Composition over configuration - don't create a god-component
// with 50 props. Instead, compose smaller focused components.
// โ Over-configured monolith
function Card({
title, subtitle, body, imageUrl, buttonText, buttonVariant,
showFooter, footerText, onButtonClick, isLoading, ...20more
}: CardProps) { ... }
// โ
Composable - consumers control what goes inside
function Card({ children }: { children: React.ReactNode }) {
return <div className="card">{children}</div>;
}
function CardHeader({ children }: { children: React.ReactNode }) {
return <div className="card-header">{children}</div>;
}
function CardBody({ children }: { children: React.ReactNode }) {
return <div className="card-body">{children}</div>;
}
// Usage - parent composes what it needs
function ProfileCard({ user }: { user: User }) {
return (
<Card>
<CardHeader>
<img src={user.avatar} alt={user.name} />
<h2>{user.name}</h2>
</CardHeader>
<CardBody>
<p>{user.bio}</p>
<Button onClick={() => console.log('follow')}>Follow</Button>
</CardBody>
</Card>
);
}
// Polymorphic component - renders as different HTML elements
interface BoxProps<T extends React.ElementType = 'div'> {
as?: T;
children: React.ReactNode;
}
function Box<T extends React.ElementType = 'div'>({
as: Component = 'div',
children,
...rest
}: BoxProps<T> & Omit<React.ComponentPropsWithoutRef<T>, keyof BoxProps<T>>) {
return <Component {...rest}>{children}</Component>;
}
// Usage:
<Box as="section" aria-label="Profile">...</Box>
<Box as="article">...</Box>Common Mistakes - Components & Props
- Using `React.FC` type - it implicitly adds `children?: ReactNode` to all components and has subtle issues with generics; use plain function types instead
- Passing objects/arrays as prop defaults inline - `<List items={[]}/>` creates a new array reference every parent render, breaking memoized children; define constants outside the component
- Creating components inside other components - a new function is created every render, React unmounts and remounts the child on every re-render, resetting all its state
- Prop drilling 3+ levels deep - instead of passing through many layers, lift state to a shared parent or use Context / Zustand
- Not naming event handler props consistently - use `onX` prefix for callbacks (`onSelect`, `onChange`, `onClose`) to signal they're event handlers, not data
Tip
Tip
Practice Functional Components Props in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Visual architecture and flow for Functional Components & Props
Practice Task
Note
Practice Task - (1) Write a working example of Functional Components Props from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Functional Components Props is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react code.
Key Takeaways
- Every React UI is made of components.
- Using `React.FC` type - it implicitly adds `children?: ReactNode` to all components and has subtle issues with generics; use plain function types instead
- Passing objects/arrays as prop defaults inline - `<List items={[]}/>` creates a new array reference every parent render, breaking memoized children; define constants outside the component
- Creating components inside other components - a new function is created every render, React unmounts and remounts the child on every re-render, resetting all its state
Finished this lesson?