Functional Components
Functional Components explains modern React function components, JSX returns, props, state, Hooks, events, composition, exports, purity, accessibility, testing, and class-component differences.
Functional Components
Functional Components are the modern and standard way to create components in React.
A functional component is essentially a JavaScript function that returns React UI.
Learning Objectives
By the end of this lesson, you will understand:
- What a functional component is
- How to create one
- How React renders a functional component
- Component naming rules
- Returning JSX
- Using JavaScript inside components
- Passing props to functional components
- Using state inside functional components
- Handling events
- Composing functional components
- Exporting and importing components
- Common mistakes
- Functional vs class components
- Functional component best practices
What Is a Functional Component?
The simplest definition is:
A functional component is a JavaScript function that returns React UI.
Example:
function Welcome() {return <h1>Welcome to React</h1>;}
That's a functional component.
The important relationship is:
JavaScript Function↓Returns JSX↓React UI
Your First Functional Component
function Greeting() {return <h1>Hello, React!</h1>;}
Let's break it down:
function↓Greeting↓()↓{return JSX}
Greeting is the component.
Rendering the Component
Creating a component doesn't automatically display it.
You need to use it:
function Greeting() {return <h1>Hello, React!</h1>;}function App() {return (<div><Greeting /></div>);}
The flow is:
App↓Greeting↓<h1>Hello, React!</h1>
Component vs Function
Every functional component is a JavaScript function, but not every JavaScript function is a React component.
Regular function:
function add(a, b) {return a + b;}
Functional component:
function Header() {return <header>My Website</header>;}
The key difference is that the component participates in React's rendering model and returns UI.
Component Naming
Functional components should normally use PascalCase.
Correct:
function Header() {return <header>Header</header>;}
Correct:
function CourseCard() {return <div>Course</div>;}
Correct:
function UserProfile() {return <div>Profile</div>;}
Avoid:
function header() {}
and:
function courseCard() {}
Prefer:
HeaderCourseCardUserProfile
Why PascalCase?
React distinguishes component names from built-in HTML elements.
For example:
<div />
represents an HTML element.
Whereas:
<CourseCard />
represents a React component.
Capitalization provides this distinction.
Returning JSX
A functional component normally returns JSX.
function Welcome() {return (<section><h1>Welcome</h1><p>Learn React step by step.</p></section>);}
The JSX describes the UI React should render.
Parentheses and JSX
When JSX spans multiple lines, parentheses make the return easier to read:
function Header() {return (<header><h1>TechLearningPro</h1><nav>Navigation</nav></header>);}
You can also write:
function Header() {return <h1>Hello</h1>;}
Both are valid.
Functional Components Can Return HTML-Like JSX
For example:
function Profile() {return (<div><h2>Student Profile</h2><p>React Developer</p></div>);}
Remember:
JSX looks like HTML, but it is JavaScript syntax that describes UI.
Functional Components Can Contain JavaScript
This is an important feature.
function User() {const name = "Jagannath";return (<h1>Hello, {name}</h1>);}
The component contains:
JavaScript+JSX
Variables Inside Components
You can define variables:
function Course() {const title = "React Fundamentals";const price = 4999;return (<div><h2>{title}</h2><p>₹{price}</p></div>);}
The {} allows JavaScript expressions to be used inside JSX.
Expressions Inside JSX
For example:
function Course() {const price = 5000;const discount = 500;return (<p>Final price: ₹{price - discount}</p>);}
Result:
Final price: ₹4500
You can use JavaScript expressions inside JSX.
Functional Components Can Receive Props
Components can accept information from their parent.
function Greeting({ name }) {return <h1>Hello, {name}</h1>;}
Then:
<Greeting name="Jagannath" />
The component receives:
name↓Greeting↓Hello, Jagannath
Props will be covered in much greater detail separately.
Props Are Function Inputs
A useful mental model:
function Greeting({ name }) {return <h1>Hello, {name}</h1>;}
Think of it like:
Props↓Function↓UI
Similar to:
function greet(name) {return `Hello, ${name}`;}
This analogy is useful when learning React.
Multiple Props
A functional component can accept multiple props.
function CourseCard({title,instructor,price}) {return (<article><h2>{title}</h2><p>{instructor}</p><strong>₹{price}</strong></article>);}
Use it:
<CourseCardtitle="React"instructor="John"price={4999}/>
Functional Components Can Have State
Modern React allows functional components to use state through Hooks.
Example:
import { useState } from "react";function Counter() {const [count, setCount] = useState(0);return (<buttononClick={() => setCount(count + 1)}>Count: {count}</button>);}
The component now has its own state.
State Flow
The basic flow is:
Initial State↓Component Renders↓User Interaction↓State Update↓React Re-renders↓Updated UI
For the counter:
count = 0↓click↓count = 1↓UI updates
Functional Components and Hooks
Hooks allow functional components to use React features.
Common Hooks include:
useStateuseEffectuseContextuseReduceruseMemouseCallbackuseRef
We'll study each of these later.
The important point now is:
Functional components can use React Hooks.
Handling Events
Functional components can define event handlers.
function LoginButton() {function handleLogin() {console.log("Login clicked");}return (<button onClick={handleLogin}>Login</button>);}
Here:
User clicks button↓onClick↓handleLogin()
Event Handler With State
import { useState } from "react";function LikeButton() {const [liked, setLiked] = useState(false);function handleClick() {setLiked(!liked);}return (<button onClick={handleClick}>{liked ? "Liked" : "Like"}</button>);}
The component contains:
State+Event+Logic+UI
This is a very common React pattern.
Conditional Rendering
Functional components can conditionally render UI.
function UserStatus({ isLoggedIn }) {if (isLoggedIn) {return <h2>Welcome back!</h2>;}return <h2>Please log in.</h2>;}
Or:
function UserStatus({ isLoggedIn }) {return (<h2>{isLoggedIn? "Welcome back!": "Please log in."}</h2>);}
Rendering Lists
Functional components commonly render arrays.
function CourseList() {const courses = ["React","Java","Spring Boot"];return (<ul>{courses.map(course => (<li key={course}>{course}</li>))}</ul>);}
The component:
Data↓map()↓JSX↓UI
Functional Components Can Call Functions
For example:
function calculatePrice(price, discount) {return price - discount;}function Course() {const finalPrice =calculatePrice(5000, 500);return (<h2>₹{finalPrice}</h2>);}
Components are still JavaScript functions, so normal JavaScript concepts remain available.
Functional Components Can Compose Other Components
This is one of their most important capabilities.
function Header() {return <header>Header</header>;}function CourseList() {return <section>Courses</section>;}function Footer() {return <footer>Footer</footer>;}function App() {return (<><Header /><CourseList /><Footer /></>);}
Component tree:
App├── Header├── CourseList└── Footer
Functional Components Can Be Nested in the Tree
For example:
function CourseCard() {return <article>Course</article>;}function CourseList() {return (<section><CourseCard /><CourseCard /></section>);}function App() {return <CourseList />;}
Tree:
App↓CourseList├── CourseCard└── CourseCard
This is composition.
Separate Component Files
A professional project generally separates meaningful components.
src/│