Thursday, June 27, 2024

Life cycle of React component

In React, the lifecycle of a class component can be divided into three main phases: mounting, updating, and unmounting.

1. Mounting: This phase occurs when an instance of a component is being created and inserted into the DOM. The lifecycle methods involved in this phase are:
  • constructor(): This is called before the component is mounted. It's where you initialize state and bind event handlers.
  • render(): This method is mandatory and it returns the JSX that represents the component.
  • componentDidMount(): This method is called after the component has been rendered into the DOM. It's often used for fetching data from APIs or setting up subscriptions.
2. Updating: This phase occurs when a component is being re-rendered as a result of changes to either its props or state. The lifecycle methods involved in this phase are:
  • shouldComponentUpdate(): This method is called before rendering when new props or state are being received. It allows you to control whether the component should re-render or not by returning a boolean value.
  • render(): Again, this method is called to re-render the component with the updated state and props.
  • componentDidUpdate(): This method is called after the component's updates are flushed to the DOM. It's often used to perform DOM operations or to fetch new data based on changes.
3. Unmounting: This phase occurs when a component is being removed from the DOM. The lifecycle method involved in this phase is:
  • componentWillUnmount(): This method is called immediately before a component is unmounted and destroyed. It's often used to clean up any DOM elements or subscriptions set up in componentDidMount().
Note:
Additionally, there are other less commonly used lifecycle methods like getDerivedStateFromProps(), getSnapshotBeforeUpdate(), componentDidCatch(), and static getDerivedStateFromError() which are used for more specific use cases like error boundaries and controlling state updates.

Is life cycle of function components possible?

Function components in React don't have lifecycle methods like class components do. However, with the introduction of React Hooks, function components can achieve similar behavior.
  • Effect Hook (useEffect): This hook replaces lifecycle methods like componentDidMount, componentDidUpdate, and componentWillUnmount. You can perform side effects such as data fetching, subscriptions, or manually changing the DOM inside a function component using useEffect.
  • State Hook (useState): This hook allows function components to have local state, similar to how this.state works in class components.
  • Context Hook (useContext): This hook allows function components to consume context values created by React.createContext.
  • Ref Hook (useRef): This hook allows function components to keep track of mutable values that persist across renders, similar to instance variables in class components.
Using these hooks, function components can achieve the same behavior as class components with lifecycle methods. Additionally, they provide a more concise and readable way to manage component logic.

No comments:

Post a Comment

Hot Topics