#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
Class Based Components

What and Why?

A class based component is a React component written as a JavaScript class that extends React.Component. Before hooks were added to React, classes were the only way to give a component its own state or to run code when it appeared on or left the screen. Today new code is usually written with function components and hooks, but class components still work, are fully supported, and fill a lot of existing codebases. Knowing how they work lets you read, maintain and gradually modernise that code.

What a class component looks like

A class component must define a render() method that returns JSX. Props arrive on this.props, state lives in this.state, and you change state by calling this.setState(), which tells React to render again.

import { Component } from 'react';

class Counter extends Component {
  state = { count: 0 };

  increment = () => {
    this.setState(prev => ({ count: prev.count + 1 }));
  };

  render() {
    return (
      <button onClick={this.increment}>
        {this.props.label}: {this.state.count}
      </button>
    );
  }
}

export default Counter;

Why you still need to learn them

  • Existing projects. Many production apps were written when classes were the standard, and you will be asked to fix bugs or add features in them.
  • Error boundaries. A component that catches rendering errors in its children must still be a class, because the methods componentDidCatch and static getDerivedStateFromError have no hook equivalent.
  • Understanding the lifecycle. Methods such as componentDidMount, componentDidUpdate and componentWillUnmount name the moments a component goes through. Seeing them spelled out makes it easier to understand what useEffect is doing for you.
  • Older tutorials and libraries. Documentation and answers you find online often use class syntax.

It also helps to know what classes do not offer. A class cannot call hooks, so you cannot use useState, useContext or a custom hook inside one. If a class component needs logic that only exists as a hook, the usual approach is to wrap it in a small function component that calls the hook and passes the result down as props.

Mapping class ideas to hooks

Class componentFunction component
this.state and this.setState()useState
componentDidMountuseEffect with an empty dependency array
componentDidUpdateuseEffect with dependencies
componentWillUnmountThe cleanup function returned from useEffect
this.propsThe function's props parameter

The confusing part: this

The biggest source of bugs in class components is the this keyword. If you write a normal method such as handleClick() { this.setState(...) } and pass it as onClick={this.handleClick}, React calls it without the component as its context, so this is undefined and you get "Cannot read properties of undefined". The fix is either to bind the method in the constructor or to define it as an arrow function class field, as increment does above.

A second trap is changing state directly with this.state.count = 5. React does not notice the change and the screen does not update. Always go through setState().

Where this leaves you

Write new components as functions, but be comfortable reading classes. Recognise render(), this.props, this.state and setState(), watch out for unbound methods, and remember that error boundaries are the one place a class is still required.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course