Functional Components in React JS
Functional Components are one of the fundamental concepts in React JS. They allow developers to create reusable, maintainable, and easy-to-understand user interface (UI) elements using JavaScript functions. A functional component can return JSX to describe what should appear on the screen.
Course: React JS Training
Course Link: JustAcademy React JS Training Course
Demo Link: Register for React JS Course Demo
1. What Are Functional Components?
A functional component is a JavaScript function that returns React elements, usually written using JSX. Components help divide a large application into smaller, reusable UI sections.
For example, a website may contain a Header, Navbar, ProductCard, Sidebar, Footer, and LoginForm. Each of these can be created as an individual functional component.
Example:
function Welcome() {
return <h1>Welcome to React JS!</h1>;
}
export default Welcome;
In this example, Welcome is a functional component. It returns JSX that displays a heading on the screen.
2. Basic Syntax of a Functional Component
A functional component is generally defined using a JavaScript function. Its name should begin with a capital letter, and it should return JSX or another valid React renderable value.
function ComponentName() {
return (
<div>
<h1>Hello World</h1>
<p>This is a functional component.</p>
</div>
);
}
export default ComponentName;
Explanation:
function ComponentName() defines the component.
return specifies the UI that the component renders.
<div>, <h1>, and <p> are JSX elements.
export default allows the component to be imported into another file.
3. Why Are Component Names Capitalized?
React uses capitalization in JSX to distinguish custom React components from built-in HTML elements.
function Greeting() {
return <h1>Hello!</h1>;
}
function App() {
return (
<div>
<Greeting />
</div>
);
}
export default App;
Correct: <Greeting /> refers to the custom React component.
Incorrect: Naming the component greeting and trying to use <greeting /> makes React treat it like a lowercase tag rather than the intended custom component.
4. How to Create a Functional Component
Step 1: Create a React Project
Create or open a React project using your preferred setup, such as Vite.
npm create vite@latest my-react-app -- --template react
cd my-react-app
npm install
npm run dev
Step 2: Create a Component File
Create a file named Welcome.jsx inside the src folder.
function Welcome() {
return <h1>Welcome to My Website</h1>;
}
export default Welcome;
Step 3: Import and Use the Component
Open App.jsx and import the component.
import Welcome from "./Welcome";
function App() {
return (
<div>
<Welcome />
</div>
);
}
export default App;
Step 4: View the Result
When the application runs, the browser displays the heading “Welcome to My Website”. React renders the component where it is used in the component tree.
5. Functional Components Using Arrow Functions
Functional components can also be written using JavaScript arrow-function syntax. Arrow functions are a common style in modern React projects.
const Greeting = () => {
return <h1>Hello from an Arrow Function!</h1>;
};
export default Greeting;
For simple components, an implicit return can be used.
const Greeting = () => <h1>Hello from React!</h1>;
export default Greeting;
Both examples define functional components. Choose the syntax that makes your code easiest to read.
6. Using JSX Inside Functional Components
JSX is a syntax extension that lets developers describe UI using markup-like syntax inside JavaScript. Functional components commonly return JSX.
function Student() {
const name = "Rahul";
const course = "React JS";
return (
<div>
<h2>Student Details</h2>
<p>Name: {name}</p>
<p>Course: {course}</p>
</div>
);
}
export default Student;
JavaScript expressions can be embedded inside JSX using curly braces. In this example, the values of name and course are displayed dynamically.
7. Returning JSX from a Functional Component
A component usually returns one JSX element or a group of elements wrapped in a parent element or React Fragment.
Using a Parent Element:
function Profile() {
return (
<div>
<h2>User Profile</h2>
<p>Welcome to your account.</p>
</div>
);
}
Using a React Fragment:
function Profile() {
return (
<>
<h2>User Profile</h2>
<p>Welcome to your account.</p>
</>
);
}
A Fragment groups elements without adding an extra DOM element. When JSX spans multiple lines after return, parentheses are commonly used to make the expression clear and avoid JavaScript automatic semicolon insertion issues.
8. Functional Components and Props
Props (short for properties) allow a parent component to pass data to a child component. A functional component receives props as its function parameter.
function Student(props) {
return (
<div>
<h2>{props.name}</h2>
<p>Course: {props.course}</p>
</div>
);
}
function App() {
return (
<div>
<Student name="Amit" course="React JS" />
<Student name="Priya" course="JavaScript" />
</div>
);
}
export default App;
The same component is reused with different prop values. Props should be treated as read-only; a component should not directly modify the props it receives.
Using Props Destructuring:
function Student({ name, course }) {
return (
<div>
<h2>{name}</h2>
<p>Course: {course}</p>
</div>
);
}
export default Student;
Destructuring extracts individual properties from the props object and can make the component code easier to read.
9. Functional Components and State
State stores information that can change over time. The useState Hook allows a functional component to manage local state and update its UI when the state changes.
import { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<h2>Counter: {count}</h2>
<button onClick={() => setCount(count + 1)}>
Increase
</button>
</div>
);
}
export default Counter;
Explanation:
useState(0) initializes the count state to zero.
count contains the current state value.
setCount requests an update to that state.
- When the state changes, React can render the component again with the updated value.
Hooks such as useState must be called at the top level of a function component or a custom Hook, not inside loops, conditions, or nested functions.
10. Event Handling in Functional Components
Functional components can respond to user actions through event handlers such as onClick, onChange, and onSubmit.
function ButtonExample() {
function handleClick() {
alert("Button clicked!");
}
return (
<button onClick={handleClick}>
Click Me
</button>
);
}
export default ButtonExample;
Here, handleClick is passed as a function reference. React calls it when the button is clicked. Avoid writing onClick={handleClick()} when you intend to run the function only after the click.
11. Conditional Rendering in Functional Components
Conditional rendering displays different UI depending on a condition. JavaScript conditional expressions and logical operators can be used inside functional components.
function LoginStatus({ isLoggedIn }) {
return (
<div>
{isLoggedIn ? (
<h2>Welcome Back!</h2>
) : (
<h2>Please Log In</h2>
)}
</div>
);
}
export default LoginStatus;
If isLoggedIn is true, the component displays “Welcome Back!”. Otherwise, it displays “Please Log In”.
12. Rendering Lists in Functional Components
The JavaScript map() method is commonly used to display arrays of data as JSX elements. Each element in a rendered list should have a stable, unique key among its siblings.
function FruitList() {
const fruits = ["Apple", "Banana", "Mango"];
return (
<ul>
{fruits.map((fruit) => (
<li key={fruit}>{fruit}</li>
))}
</ul>
);
}
export default FruitList;
The component maps each fruit to a list item. For data that can change, use a stable identifier from the data rather than relying on array indexes whenever possible.
13. Reusing Functional Components
One of the biggest advantages of functional components is reusability. A component can be used in multiple places instead of repeating the same markup.
function ProductCard({ name, price }) {
return (
<div>
<h3>{name}</h3>
<p>Price: ₹{price}</p>
<button>View Product</button>
</div>
);
}
function App() {
return (
<div>
<ProductCard name="Keyboard" price={999} />
<ProductCard name="Mouse" price={499} />
<ProductCard name="Monitor" price={7999} />
</div>
);
}
export default App;
The ProductCard component defines a reusable structure, while props provide the details for each product.
14. Parent and Child Functional Components
React applications often organize components into a parent-child hierarchy. A parent component can render child components and pass information to them through props.
function Header() {
return <h1>My Website</h1>;
}
function Content() {
return <p>Welcome to the home page.</p>;
}
function Footer() {
return <footer>Copyright 2026</footer>;
}
function App() {
return (
<div>
<Header />
<Content />
<Footer />
</div>
);
}
export default App;
The App component acts as the parent and renders the Header, Content, and Footer child components. In typical projects, define components at the top level of a module rather than declaring them inside another component.
15. Functional Components with Default Props
Default parameter values can provide fallback values when a prop is missing or explicitly undefined.
function Greeting({ name = "Guest" }) {
return <h2>Hello, {name}!</h2>;
}
function App() {
return (
<div>
<Greeting name="Ravi" />
<Greeting />
</div>
);
}
export default App;
The first greeting displays “Hello, Ravi!”, while the second displays “Hello, Guest!”.
16. Functional Components and React Hooks
Hooks let functional components use React features such as state, context, and effects. Common Hooks include:
useState – manages component state.
useEffect – synchronizes a component with external systems when needed.
useContext – reads a value from React context.
useRef – stores a mutable reference or accesses a DOM node.
useMemo – caches a calculated value when appropriate.
useCallback – caches a function definition when appropriate.
Hooks should be used according to their rules. Not every component needs Hooks; a component that only displays fixed content may be a simple function returning JSX.
17. Functional Components vs Class Components
| Feature |
Functional Component |
Class Component |
| Definition |
JavaScript function |
JavaScript class extending React.Component |
| UI output |
Usually returns JSX |
Usually returns JSX from render() |
| State |
Managed with Hooks such as useState |
Managed with this.state and this.setState |
| Side effects |
Often managed with useEffect when needed |
Often managed with lifecycle methods |
| Syntax |
Usually concise |
Typically more boilerplate |
| Use in new code |
Generally recommended |
Still supported, but less commonly recommended for new components |
Functional components are generally preferred for new React code because Hooks provide a direct way to use state and other React features without class syntax.
18. Advantages of Functional Components
- Simple syntax: Components can be defined using ordinary JavaScript functions.
- Reusability: The same component can render different data through props.
- Maintainability: Smaller components can be easier to understand and update.
- Hooks support: Components can use state, context, and other React features through Hooks.
- Separation of concerns: UI can be divided into logical parts such as Header, Form, and Footer.
- Testing: Small components can be tested independently.
- Composition: Components can be combined to build larger pages and application layouts.
19. Limitations and Important Considerations
- A component must begin with a capital letter when used as a custom JSX component.
- Props should not be modified directly.
- State should be updated using its setter function rather than direct mutation.
- Hooks must follow the Rules of Hooks.
- Rendering should remain free of side effects; perform side effects in event handlers or appropriate Effects.
- Large components can become difficult to maintain, so split them into smaller components when appropriate.
- Memoization is not automatically necessary; use optimization tools when there is a demonstrated reason.
20. Common Mistakes in Functional Components
| Mistake |
Why It Is a Problem |
Correct Approach |
| Using a lowercase custom component name |
JSX treats lowercase names as built-in or custom HTML-like tags. |
Use a capitalized name such as Welcome. |
| Forgetting to return JSX |
The component may render nothing. |
Return the intended JSX. |
| Returning adjacent JSX elements without a wrapper |
The JSX syntax is invalid. |
Wrap them in a parent element or Fragment. |
| Calling an event handler during rendering |
The handler runs immediately rather than on the event. |
Pass a reference such as onClick={handleClick}. |
| Changing props directly |
Props are inputs and should be treated as read-only. |
Ask the parent to provide updated props or use local state where appropriate. |
| Mutating state directly |
React may not detect the intended state update correctly. |
Use the state setter and immutable updates. |
| Defining a component inside another component |
It can create a new component type on each render and lead to state or performance problems. |
Define components at module scope and pass data through props. |
21. Practical Example: Student Information Card
This example combines functional components, props, JSX, and reusable UI.
function StudentCard({ name, course, duration }) {
return (
<div>
<h2>Student Information</h2>
<p>Name: {name}</p>
<p>Course: {course}</p>
<p>Duration: {duration}</p>
</div>
);
}
function App() {
return (
<div>
<StudentCard
name="Aarav"
course="React JS"
duration="3 Months"
/>
<StudentCard
name="Ananya"
course="Frontend Development"
duration="4 Months"
/>
</div>
);
}
export default App;
Expected Result:
- The first card displays Aarav's student information.
- The second card displays Ananya's student information.
- Both cards use the same component structure with different props.
22. Practical Exercise
Create a simple React application using functional components with the following requirements:
- Create a
Header component that displays the website name.
- Create a
Profile component that displays a person's name and role.
- Create a
CourseCard component that accepts a course name and price through props.
- Create a
Counter component using the useState Hook.
- Create a
Footer component with copyright information.
- Import and render all these components inside
App.jsx.
- Check that each component displays the expected content in the browser.
23. Interview Questions and Answers
Q1. What is a functional component in React?
Answer: A functional component is a JavaScript function that returns React elements, commonly described using JSX, to display part of a user interface.
Q2. How do you create a functional component?
Answer: Define a JavaScript function with a capitalized name and return the desired JSX. Export it if it needs to be imported into another module.
Q3. Can functional components receive props?
Answer: Yes. Props are passed by the parent component and received as the function's parameter.
Q4. Can functional components manage state?
Answer: Yes. They can use the useState Hook to maintain local state and update the UI.
Q5. What is the difference between props and state?
Answer: Props are inputs supplied by a parent component, while state is data managed by the component or Hook that owns it.
Q6. Why should a component name start with a capital letter?
Answer: React uses capitalization in JSX to distinguish custom components from built-in HTML elements.
Q7. Can a functional component render another component?
Answer: Yes. Components can be nested and composed using JSX, allowing an application to be built from smaller UI units.
Q8. What are React Hooks?
Answer: Hooks are functions that let function components use React features such as state, context, and effects.
Q9. What does a functional component return?
Answer: It commonly returns JSX describing the UI, although React supports other valid renderable return values too.
Q10. Are class components still supported?
Answer: Yes. Class components are still supported, but React generally recommends function components for new code.
24. Quick Revision Checklist
- Understand the definition and purpose of functional components.
- Know how to create and export a component.
- Use capitalized names for custom components.
- Understand JSX return syntax and Fragments.
- Pass data using props and destructuring.
- Manage changing data with useState.
- Handle user interactions with event handlers.
- Render conditions and lists correctly.
- Use stable keys for elements in lists.
- Follow the Rules of Hooks and keep rendering free of side effects.
25. Conclusion
Functional components are a core part of modern React development. They let developers create reusable UI elements using JavaScript functions and JSX. By learning component syntax, props, state, event handling, conditional rendering, and Hooks, developers can build organized and interactive React applications.
To learn more about React JS training, visit JustAcademy React JS Training Course.
To explore a course demonstration, visit Register for React JS Course Demo.