In this module, you'll learn about class components in React and how they differ from functional components. You'll understand the lifecycle methods and state management in class components.
Class components have been a significant part of the React ecosystem because they provide functionality that wasn't initially available with functional components. Though hooks now bring similar capabilities to functional components, many existing projects still use class components, making it essential to understand how to work with them.
The focus will be on understanding React's Component
base class, which allows access to lifecycle methods, state management, and event handling in a class-based approach.
Remember the "CCR" approach to building class components:
React.Component
base classsuper()
in the constructor to access this
this.state
this.setState()
methodconstructor()
- Initialize state and bind methodsrender()
- Return JSX to be renderedcomponentDidMount()
- Runs after component is mounted to the DOM, perfect for data fetching