PGoCareerGoCareer prep tools
Home
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

React · Theory

Class Components in React

← All stacks

Theory

85/94

Class Components in React

A class component extends React.Component, keeps data in this.state, updates with this.setState, and draws in render(). Lifecycle methods: componentDidMount, componentDidUpdate, componentWillUnmount. You will still read this in older codebases.

Map to hooks: state → useState, didMount/didUpdate/willUnmount → useEffect. Don’t start a new screen as a class unless the team’s file already is one. Interview: show you can read a class, then say you’d rewrite it as a function.

Class Components in React — on screen — button 0, click → 1. Class uses this.setState. Today we write useState instead.

Diagram
Parent
    │ props (read-only)
    ▼
  Child → UI
Exam tip

Read class. Prefer hooks for new code.

Example

// Class component
import { Component } from "react";

class Counter extends Component {
  state = { n: 0 };
  render() {
    return (
      <button onClick={() => this.setState({ n: this.state.n + 1 })}>
        {this.state.n}
      </button>
    );
  }
}

Class Components in React — on screen: button 0, click → 1. Class uses this.setState. Today we write useState instead.

Short notes

  • Defclass + this.state + render().
  • Map : didMount ≈ useEffect([]).
  • Now : function components.

Questions

1

Explain Class Components as if you are teaching a junior — definition, then one tiny UI.

2

What does the example show on screen (or print), and what does that prove?

3

What mistake do freshers make with Class Components?

Previous← useState ExampleNextReact Basics →
P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.