In the previous chapter, we have seen about the React component and component types. In this chapter, we will learn about the React functional component with an example.
Functional Component Overview
- Functional components are basic JavaScript functions. These are typically arrow functions but can also be created with the regular function keyword.
- Sometimes referred to as “dumb” or “stateless” components as they simply accept data and display them in some form; that is they are mainly responsible for rendering UI.
- React lifecycle methods (for example, componentDidMount) cannot be used in functional components.
- There is no render() method used in functional components.
- These are mainly responsible for UI and are typically presentational only (For example, a Button component).
- Functional components can accept and use props.
- Functional components should be favored if you do not need to make use of the React state.
import React from "react";
const User = props => (
<div>
<h1>Hello, {props.name}</h1>
</div>
);
export default User;
Functional Component Example
TableHeader.js
import React from 'react'
export default function TableHeader() {
return (
<thead>
<th> Employee Name </th>
<th> Employee Role </th>
</thead>
)
}
TableBody.js
import React from 'react'
export default function TableBody() {
return (
<tbody>
<tr>
<td> Ramesh</td>
<td> Software Developer</td>
</tr>
<tr>
<td> Tony</td>
<td> Software Developer</td>
</tr>
<tr>
<td> Pramod</td>
<td> Software Developer</td>
</tr>
<tr>
<td> Santosh</td>
<td> QA Engineer</td>
</tr>
</tbody>
)
}
Table.js
import React from 'react'
import TableHeader from './TableHeader'
import TableBody from './TableBody'
export default function Table() {
return (
<div>
<table border = "1">
<TableHeader />
<TableBody />
</table>
</div>
)
}
App.js
import React from 'react';
import logo from './logo.svg';
import './App.css';
import Table from './components/functional-components/Table';
function App() {
return (
<div className="App">
<header className="App-header">
<Table />
</header>
</div>
);
}
export default App;
ES6 Arrow Functions
import React from 'react'
export const TableHeader = () => {
return (
<thead>
<th> Employee Name </th>
<th> Employee Role </th>
</thead>
)
}
Demo
What's Next?
❮ Previous Chapter Next Chapter ❯
Continue Learning with Java Guides
Want to take your programming skills to the next level? Explore my practical Udemy courses on Java, Spring Boot, Microservices, Full-Stack Development, Docker, and Generative AI. You can also follow my structured learning roadmaps to become a professional developer.
By Ramesh Fadatare - Udemy Instructor Partner

Comments
Post a Comment
Leave Comment