📘 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 understood functional components and in this chapter, we will learn about class components in React with an example.
Class Component Overview
- Class components make use of ES6 class and extend the Component class in React.
- Sometimes called “smart” or “stateful” components as they tend to implement logic and state.
- React lifecycle methods can be used inside class components (for example, componentDidMount).
- You pass props down to class components and access them with this.props
import React, { Component } from "react";
class User extends Component {
constructor(props){
super(props);
this.state = {
myState: true;
}
}
render() {
return (
<div>
<h1>Hello Ramesh</h1>
</div>
);
}
}
export default User;
Class Component Example
TableHeader.js
import React, { Component } from 'react'
export default class TableHeader extends Component {
render() {
return (
<thead>
<th> Employee Name </th>
<th> Employee Role </th>
</thead>
)
}
}
TableBody.js
import React, { Component } from 'react'
export default class TableBody extends Component {
render() {
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, { Component } from 'react'
export default class Table extends Component {
render() {
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;
Comments
Post a Comment
Leave Comment