REACTLIFECYCLE
REACTLIFECYCLE
Before Hooks, React components used classes to manage state and lifecycle events. Every
class component in React goes through a lifecycle that consists of three main phases:
1.Mounting: When the component is being created and inserted into the DOM.
2.Updating: When the component is being re-rendered due to changes in props or state.
Mounting Phase
During the mounting phase, the component is created and added to the DOM. React provides
•constructor(): This is the first method that runs in a component’s lifecycle. It is mainly
used for fetching data from an API or interacting with the DOM.
Updating Phase
In the updating phase, a component re-renders in response to changes in state or props. The
component has updated. It allows you to perform actions after the component has been re-
componentDidUpdate(prevProps, prevState) {
Unmounting Phase
The unmounting phase occurs when the component is removed from the DOM. The only
from the DOM. It’s useful for cleaning up resources like timers or event listeners.
componentWillUnmount() {
clearInterval(this.timer);
React Hooks, introduced in React 16.8, allow functional components to manage state and
lifecycle events, eliminating the need for class components in most cases. The two most
essential hooks that replace lifecycle methods are useState and useEffect.
The useState Hook allows you to add state to functional components. It returns an array
Example:
function Counter() {
return (
<div>
Click me
</button>
</div>
);
Here, useState(0) initializes the state variable count with a value of 0. The setCount function is
perform side effects, such as data fetching or interacting with the DOM, in functional
components.
Example:
In this example:
•The useEffect Hook is used to fetch data when the component mounts.
•The empty dependency array ([]) ensures the effect only runs on mount and unmount.
•The cleanup function (return () => console.log('Cleanup'))
mimics componentWillUnmount.
Lifecycle methods are exclusive to class components, while Hooks bring similar
2.Multiple Effects:
With Hooks, you can have multiple useEffect calls to handle different side effects, whereas
lifecycle methods combine everything into a single method (e.g., componentDidMount for
3.Simplified Code:
Hooks allow for more concise and readable code compared to lifecycle methods, especially
Class components require binding this to access state and props, which can lead to
confusion. Hooks eliminate this issue, making functional components easier to work with.
React encourages developers to use Hooks over class components for new projects due to their
simplicity and flexibility. However, you may still encounter class components in legacy
codebases, and understanding lifecycle methods is essential for maintaining such projects.
•Use Hooks for managing state and side effects in functional components.
•Use Lifecycle Methods in class components only if you’re working with older codebases
React lifecycle methods and Hooks are fundamental concepts for managing a component’s
state and side effects throughout its existence. While class components rely on lifecycle
methods to handle various phases, Hooks provide a more modern, functional approach that