Functional Components
Functional Components
Functional components are JavaScript functions that return JSX.
Basic Component
function Greeting() {
return <h1>Hello, World!</h1>;
}
// Arrow function
const Greeting = () => {
return <h1>Hello, World!</h1>;
};
// Implicit return
const Greeting = () => <h1>Hello, World!</h1>;
Component with Props
function Greeting({ name, age }) {
return (
<div>
<h1>Hello, {name}!</h1>
<p>You are {age} years old.</p>
</div>
);
}
// Usage
<Greeting name="Alice" age={30} />
Component Rules
- Name components with PascalCase
- Export components for reuse
- One component per file
- Return JSX or null
// Good
function UserProfile({ user }) {
return (
<div>
<h2>{user.name}</h2>
<p>{user.email}</p>
</div>
);
}
export default UserProfile;
Component File Structure
Component File Structure
Single File Component
// Button.jsx
import React from "react";
function Button({ label, onClick }) {
return (
<button onClick={onClick} className="btn">
{label}
</button>
);
}
export default Button;
Component with Styles
// Button.jsx
import React from "react";
import "./Button.css";
function Button({ label, onClick, variant = "primary" }) {
return (
<button
onClick={onClick}
className={`btn btn-${variant}`}
>
{label}
</button>
);
}
export default Button;
Folder Structure
src/
components/
Button/
Button.jsx
Button.css
Button.test.jsx
index.js
Card/
Card.jsx
Card.css
Header/
Header.jsx
Header.css
Index Files
// components/Button/index.js
export { default } from "./Button";
// Usage
import Button from "./components/Button";
Component Composition
Component Composition
Compose components together to build complex UIs.
Basic Composition
function App() {
return (
<div className="app">
<Header />
<main>
<Sidebar />
<Content />
</main>
<Footer />
</div>
);
}
Passing Components as Props
function Card({ title, children }) {
return (
<div className="card">
<h2>{title}</h2>
<div className="card-body">{children}</div>
</div>
);
}
function App() {
return (
<Card title="My Card">
<p>This is card content</p>
<Button label="Click me" />
</Card>
);
}
Layout Components
function Layout({ children }) {
return (
<div className="layout">
<header>Logo</header>
<main>{children}</main>
<footer>© 2024</footer>
</div>
);
}
// Usage
<Layout>
<HomePage />
</Layout>
Composition vs Inheritance
React favors composition over inheritance:
// Good: Composition
function Dialog({ title, children }) {
return (
<div className="dialog">
<h2>{title}</h2>
{children}
</div>
);
}
function ConfirmDialog({ message, onConfirm }) {
return (
<Dialog title="Confirm">
<p>{message}</p>
<button onClick={onConfirm}>Yes</button>
</Dialog>
);
}
Practice Problems
Create a reusable React component implementing Components. Include proper state management and accessibility.
Solution
// Production-ready component with:
// - Proper TypeScript types
// - Accessibility (ARIA)
// - Error boundaries
// - Loading states
// - Memoization where neededWrite unit and integration tests for Components using React Testing Library.
Solution
// Test coverage:
// 1. Rendering tests
// 2. Interaction tests
// 3. Edge case tests
// 4. Accessibility testsOptimize Components for performance. Consider memoization, code splitting, and bundle size.
Solution
// Optimization techniques:
// 1. React.memo / useMemo / useCallback
// 2. Code splitting with lazy()
// 3. Virtual scrolling for lists
// 4. Image lazy loading
// 5. Bundle analysisQuiz
1. What is the naming convention for React components?
2. What must a functional component return?
3. What is component composition?
4. What is the children prop used for?
Flashcards
Question
What is a functional component?
Click to reveal answer
Answer
A JavaScript function that returns JSX
Question
How do you name React components?
Click to reveal answer
Answer
With PascalCase
Question
What is the children prop?
Click to reveal answer
Answer
A special prop that contains nested components
Question
What is component composition?
Click to reveal answer
Answer
Building complex UIs by combining smaller components
Question
What is Components?
Click to reveal answer
Answer
Components is a key concept in frontend development.
Revision Notes
Key Takeaways
- 1.Functional components are functions that return JSX
- 2.Use PascalCase for component names
- 3.Components can be composed together
- 4.The children prop enables nesting
- 5.One component per file is best practice
Interview Tips
- •Show how to create a basic functional component
- •Explain component composition
- •Demonstrate how to use the children prop
Cheat Sheet
Cheat Sheet
Functional Component
function Greeting({ name }) {
return <h1>Hello, {name}!</h1>;
}
Component Rules
- PascalCase naming
- Return JSX or null
- One component per file
- Export for reuse
Composition
function App() {
return (
<Layout>
<Header />
<Content />
<Footer />
</Layout>
);
}
Children Prop
function Card({ title, children }) {
return <div><h2>{title}</h2>{children}</div>;
}