Class Components are one of the two types of components in React. They were the main way to create components before Functional Components became popular with the introduction of React Hooks. Class Components are based on ES6 classes and are more complex compared to Functional Components.
You should know about Class Components because:
Here’s how you create a basic Class Component:
import React, { Component } from 'react';
class Welcome extends Component {
render() {
return (
<div>
<h1>Welcome to My React App</h1>
</div>
);
}
}
export default Welcome;
Welcome
that extends Component. This means our Welcome class inherits properties and methods from React's Component.render
method. This method returns the JSX that defines what the component will display. In this example, it simply returns a <div>
with a heading.export default
to make our component available for use in other parts of the application.One of the main features of Class Components is state. State is used to store information that can change over time and affect how the component behaves or looks.
Here’s an example of a Class Component with state:
import React, { Component } from 'react';
class Counter extends Component {
constructor(props) {
super(props);
this.state = {
count: 0
};
}
increment = () => {
this.setState({ count: this.state.count + 1 });
};
render() {
return (
<div>
<h1>Count: {this.state.count}</h1>
<button onClick={this.increment}>Increment</button>
</div>
);
}
}
export default Counter;
super(props)
to ensure the parent Component class is properly initialized. this.state
is used to set the initial state.this.state
is an object where we store the component's data. In this case, we start with count set to 0.this.setState
to update the state. When this.setState
is called, React re-renders the component with the new state. Here, the increment method increases the count by 1.this.state.count
and display it in the <h1>
tag. The button uses an onClick
event to call the increment method when clicked.Class Components have special methods called lifecycle methods that you can use to run code at specific points in a component's life. These methods allow you to handle things like data fetching, manual DOM manipulation, and cleanup.
Here’s an example of a Class Component using a lifecycle method:
import React, { Component } from 'react';
class LifecycleExample extends Component {
componentDidMount() {
console.log('Component has been rendered to the DOM');
}
render() {
return (
<div>
<h1>Lifecycle Example</h1>
</div>
);
}
}
export default LifecycleExample;
componentDidMount
is a lifecycle method that runs after the component has been rendered to the DOM. It’s often used to fetch data or perform setup tasks.Functional Components have become the preferred way to create components in React, thanks to their simplicity and the introduction of React Hooks. Hooks allow you to use state and other features without writing a class.
Class Components were the primary way to create components in React before Functional Components and Hooks were introduced. They use classes and have features like state and lifecycle methods. Although you will mainly work with Functional Components in modern React development, understanding Class Components is useful for reading and maintaining older code.