Replacing Lifecycle methods with React Hooks

Search for a command to run...

No comments yet. Be the first to comment.
💡 “So if you want to go fast, if you want to get done quickly, if you want your code to be easy to write, make it easy to read.” -By Robert C. Martin JavaScript has progressed a ton in recent years. If you are learning JavaScript in 2022 and you ...

Before beginning our quest to find how not to use Create React App. First, let’s see why we need Create React App in the first place. Why do we need Create React App? 🤔 Create React App is a comfortable environment for learning React and is the best...

Getting Started with Node.js In this blog, we will try to understand the basics of Node.js, how it works, and why to choose Node.js, but before diving into node let's talk a little bit about Java-script. JavaScript is one of the best programming lang...

If you are reading this, you probably know what React.js is and might have already used it Earlier. However, you might be wondering why I am reading about folder structure? Can’t I just stuff all my files in the src folder?? 🤪 Technically you could ...

Let’s first understand 🤔 What is an Array? Quoting from MDN directly. The Array object, as with arrays in other programming languages, enables storing a collection of multiple items under a single variable name and has members for performing common...

React lifecycle methods are the events or scenarios that happen while your component is rendered for the first time (birth) and discarded after its use (death). By default, this method can’t be used in functional components directly.
Note: React 16.8.0 is the first release to support Hooks.
- Lifecycle events in React play a crucial role while writing React components.
- We as Developers often need lifecycle events to handle various effects such as fetching data on the mount, cleaning up before the component unmounts, sanitizing props when the component updates, etc.
Here’s the entire lifecycle of a component:

See in React, components go through a lifecycle of events (Different phases in the lifecycle):
Mounting ( When the component is initially rendered in DOM )Updating (When the component is being updated through states or props)Unmounting (When the component is removed from the DOM)I can understand it sounds high Mounting, Updating, Unmounting 😃 😃 but You can think of these events as a component’s birth, growth, and death, respectively.
States
A set of data that an individual component holds.
Props
Props are similar to the argument passed to a function. The component accepts props to render contents conditionally based on the props provided.
Or data that is being passed from one component to another.
It has four built-in methods that are called in this order
const MyComponent extends React.Component {
constructor(props) {
super(props)
this.state = {
name: "pankaj"
}
this.handlePoints = this.handlePoints.bind(this)
}
}
Before (class-based component):
import React from "react";
class Component extends React.Component {
componentDidMount() {
//logic
}
render() {
return <h1>Hello</h1>;
}
};
After (functional component using Hooks):
import React, { useEffect } from "react";
const Component = () => {
useEffect(() => { //the useEffect Hook will be invoked when the component mounts.
//logic
}, []); // Pass an empty array to run only callback on mount only.
return <h1>Hello World</h1>;
};
An update can be caused by changes to props or state. These methods are called in the following order when a component is being re-rendered:
static getDerivedStateFromProps()shouldComponentUpdate()shouldComponentUpdate(nextProps, nextState)
getSnapshotBeforeUpdate()// Called after render() but before updating the DOM
// A good time to make calculations based on old DOM nodes.
// The value returned here is passed into componentDidUpdate
getSnapshotBeforeUpdate(nextProps, nextState) {
console.log('[getSnapshotBeforeUpdate]', 'About to update...');
return `Time is ${Date.now()}`;
}
componentDidUpdate()Before (class-based component):
import React from "react";
class Component extends React.Component {
componentDidUpdate() {
//logic
}
render() {
return <h1>Hello</h1>;
}
};
After (functional component using Hooks):
import React, { useEffect } from "react";
const Component = () => {
useEffect(() => {
//logic
});
return <h1>Hello World</h1>;
};
This looks very similar to how we handled componentDidMount........
What is the difference??
This method is called when a component is being removed from the DOM:
componentWillUnmount()componentWillUnmount()Before (class-based component):
import React from "react";
class Component extends React.Component {
componentWillUnmount() {
//logic
}
render() {
return <h1>Hello</h1>;
}
};
After (functional component using Hooks):
import React, { useEffect } from "react";
const Component = () => {
useEffect(() => {
return () => {
console.log("return right before the component is removed from the DOM.");
}
}, []);
return <h1>Hello World</h1>;
};
This again looks very similar to how we handled componentDidMount........