What are Lifecycle Methods in React

Need -

Lifecycle methods are essential for controlling and managing various stages of a component's lifecycle, such as initialization, update, and clean-up.

What is it?

Lifecycle methods are special functions in React Class components that automatically get invoked during the different stages of a component's lifecycle.

How is it used?

Mounting:

constructor: Initializes state and binds methods. It's called before the component mounts.

render: Returns the JSX to render. It's invoked before the component mounts and on every subsequent update.

componentDidMount: Commonly used for API calls and setting timers. It's invoked immediately after a component is mounted.

Updating:

shouldComponentUpdate: Decides if a component should re-render on receiving new props or state. It's called before rendering when new props or state are being received.

render: Returns the updated JSX. It's called on the initial render and every subsequent update.

componentDidUpdate: Handles updates, like making API calls based on prop changes. It's invoked immediately after updating occurs.

Unmounting:

componentWillUnmount: Cleans up any leftover data like invalidating timers or canceling network requests. It's called immediately before a component is unmounted and destroyed.