📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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 ❯
Comments
Post a Comment
Leave Comment