useEffect() Hook In ReactJS - Part One

Introduction

 
In the previous article, we learned about useState hook along with its various examples and usage with the functional components. In this article, we will learn about useEffect() hooks and how to use them in React.
 

useEffect() in React

 
The React useEffect hook allows adding side-effects in a functional component.
 
Previously, in-class component, we saw that to perform side effects, we need to use component life cycle methods like componentDidMount(), componentDidUpdate() and componentWillMount(). In class component, when any update is needed via the lifecycle, we need to call it twice, but this is not the case when using Hooks.
 
For example, to update the title of a document in the browser, we have the below code using class component.
  1. import React, { Component } from 'react'      
  2. class UseEffectDemo extends Component {     
  3.     constructor(props) {      
  4.         super(props)      
  5.         this.state = {      
  6.             title: 'React'      
  7.         }      
  8.         this.updateTitle = this.updateTitle.bind(this)      
  9.     }      
  10.     updateTitle(e) {      
  11.         this.setState({      
  12.             title: e.target.value      
  13.         })      
  14.         e.preventDefault();      
  15.     }      
  16.     componentDidMount() {      
  17.         document.title = `New ${this.state.title}`      
  18.     }      
  19.     componentDidUpdate() {      
  20.         document.title = `New ${this.state.title}`      
  21.     }      
  22.     render() {      
  23.         return (      
  24.             <div>      
  25.                 <input type="text" value={this.state.title} onChange={this.updateTitle}></input>      
  26.             </div>      
  27.         )      
  28.     }      
  29. }      
  30. export default UseEffectDemo    
This will display an output as below.
 
 
As we type further, the title is getting updated automatically.
 
Now, we will look at the same demo using useEffect() Hook.
 
Let us create a function in UseEffectHook.js.
  1. import React, { useState, useEffect } from 'react'      
  2. function UseEffectFunction() {      
  3.     const initialTitle = 'React'      
  4.     const [title, setTitle] = useState(initialTitle);      
  5.     useEffect(() => {      
  6.         document.title = `New ${title}`      
  7.     })      
  8.     return (      
  9.         <div>      
  10.             <input type="text" value={title} onChange={(e) => setTitle(e.target.value)} />      
  11.         </div>      
  12.     )      
  13. }      
  14. export default UseEffectFunction    
This will display the same output as a class component.
 
 
Initially, it will display the value as React.
 
 
Later, as we type in the textbox, the same value will get updated in the title. So, the useEffect() hook will run on first, as well as, every render to update the values as per your requirement.
 
One thing to remember while using useEffect() hook is that it should be defined within the component. This will help to access all properties and the state of component without additional code.
 
Now, we will run useEffect() on every render. It should run conditionally.
 

useEffect() call conditionally

 
Now, let’s check how to not re-render unnecessarily in both, class as well as a functional component.
  1. import React, { Component } from 'react'      
  2. class UseEffectDemo extends Component {      
  3.     constructor(props) {      
  4.         super(props)      
  5.         this.state = {      
  6.             title: 'React'    
  7.           }    
  8.         this.updateTitle = this.updateTitle.bind(this)      
  9.     }      
  10.     updateTitle(e) {      
  11.         this.setState({      
  12.             title: e.target.value,      
  13.             count: 0      
  14.         })      
  15.         e.preventDefault();      
  16.     }      
  17.     componentDidMount() {      
  18.         document.title = `New ${this.state.title}`      
  19.     }      
  20.     componentDidUpdate(prevProps, prevState) {      
  21.         console.log("Render componentDidUpdate")      
  22.         document.title = `New ${this.state.title}`      
  23.     }      
  24.     render() {      
  25.         return (      
  26.             <div>      
  27.                 <label>Button Clicked {this.state.count}</label>      
  28.                 <input type="text" value={this.state.title} onChange={this.updateTitle}></input>      
  29.                 <button onClick={() => this.setState({ count: this.state.count + 1 })}>Click Me</button>      
  30.             </div>      
  31.         )      
  32.     }      
  33. }      
  34. export default UseEffectDemo    
In the output, when we write in textbox, the render method is called. Now, on click of the button, it is again rendering, which is not required.
 
So, in the console, we can see that render is called multiple times.
 
Now, we will restrict the assignment of new value every time.
  1. import React, { Component } from 'react'      
  2. class UseEffectDemo extends Component {   
  3.     constructor(props) {      
  4.         super(props)      
  5.         this.state = {      
  6.             title: 'React'      
  7.         }      
  8.         this.updateTitle = this.updateTitle.bind(this)      
  9.     }      
  10.     updateTitle(e) {      
  11.         this.setState({      
  12.             title: e.target.value,      
  13.             count: 0      
  14.         })      
  15.         e.preventDefault();      
  16.     }      
  17.     componentDidMount() {      
  18.         document.title = `New ${this.state.title}`      
  19.     }      
  20.     componentDidUpdate(prevProps, prevState) {      
  21.         if (prevState.title !== this.state.title) {      
  22.             console.log("Render componentDidUpdate")    
  23.             document.title = `New ${this.state.title}`    
  24.         }      
  25.     }    
  26.       render() {    
  27.         return (      
  28.             <div>      
  29.                 <label>Button Clicked {this.state.count}</label>      
  30.                 <input type="text" value={this.state.title} onChange={this.updateTitle}></input>      
  31.                 <button onClick={() => this.setState({ count: this.state.count + 1 })}>Click Me</button>      
  32.             </div>      
  33.         )      
  34.     }      
  35. }      
  36. export default UseEffectDemo    
The output will be as below.
 
Now, we will look at the same code for Functional component.
  1. import React, { useState, useEffect } from 'react'      
  2. function UseEffectFunction() {      
  3.     const initialTitle = 'React'      
  4.     const [title, setTitle] = useState(initialTitle);      
  5.     const [count, setCount] = useState(0)      
  6.     useEffect(() => {      
  7.         console.log("useEffect rendered")      
  8.         document.title = `New ${title}`      
  9.     })      
  10.     return (      
  11.         <div>      
  12.             <label>Button clicked {count}</label>      
  13.             <input type="text" value={title} onChange={(e) => setTitle(e.target.value)} />      
  14.             <button onClick={() => setCount(count + 1)}>Click Me</button>    
  15.         </div>      
  16.     )      
  17. }      
  18. export default UseEffectFunction    
The output will be as below.
 
So, after clicking on the button, useEffect() is called every time.
 
To prevent it from happening, we have the functionality within useEffect() function.
 
We can pass a variable within it, as shown in the code below.
  1. import React, { useState, useEffect } from 'react'      
  2. function UseEffectFunction() {      
  3.     const initialTitle = 'React'      
  4.     const [title, setTitle] = useState(initialTitle);      
  5.     const [count, setCount] = useState(0)      
  6.     useEffect(() => {      
  7.         console.log("useEffect rendered")      
  8.         document.title = `New ${title}`      
  9.     }, [title])      
  10.     return (      
  11.         <div>      
  12.             <label>Button clicked {count}</label>      
  13.             <input type="text" value={title} onChange={(e) => setTitle(e.target.value)} />      
  14.             <button onClick={() => setCount(count + 1)}>Click Me</button>    
  15.         </div>      
  16.     )      
  17. }      
  18. export default UseEffectFunction    
The output will be displayed as below.
 

Summary

 
In this article, we have learned the concept of useEffect(), how it is used or defined, and how it can be implemented to call conditionally.
 
You can download the source code attached to this article. In the next article, we are going to learn about how to use the useEffect() hook to call only once is some scenarios and use it with cleanup which will prevent a memory leak.