How To Avoid Infinite Loops When Using useEffect() in ReactJS?
Last Updated :
14 Oct, 2024
useEffect() can lead to infinite loops, causing performance issues or crashes if not used correctly. In this article, we will explain what causes these infinite loops and how to avoid them when working with useEffect() in ReactJS.
Avoid infinite loops in useEffect() by providing appropriate dependencies in the dependency array. Use an empty array for one-time execution, a specific state value for triggering effect, or multiple state values with conditions for selective execution.
Prerequisites
To Avoid Infinite Loops When using the useEffect() in ReactJS we have these approaches:
Steps To Avoid Infinite Loops When Using useEffect()
Step 1: Create a React project:
npx create-react-app appname
Step 2: After creating your project folder i.e. appname, move to it using the following command:
cd appname
Project Structure

Now, we will see some examples to avoid or prevent the infinite loops when using the useEffect() hook with a different approach.
Approach 1: Using Empty Dependency Array
In this example, we are fetching data from an API using the useEffect hook. We pass an empty array as the dependency array, which means that the hook will only be called once after the component mounts.
If we didn’t specify any dependencies, the hook would be called after every render, causing an infinite loop. By specifying the empty array as the dependency, we ensure that the hook is only called once, and we avoid any performance issues or crashes.
Step to Install axios module:
npm i axios
The updated dependencies in package.json file.
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"axios": "^1.6.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}
Example: Below example demonstrates how to avoid infinite loops when using the useEffect hook in React.
JavaScript
// Filename - App.js
import React, {
useState,
useEffect
} from 'react';
import axios from 'axios';
function App() {
const [data, setData] = useState([]);
const [loading, setLoading] = useState(false);
useEffect(() => {
setLoading(true);
axios.get(
'https://...com/posts')
.then((response) => {
setData(response.data);
setLoading(false);
})
.catch((error) => {
console.log(error);
setLoading(false);
});
}, []); // This is the dependency array
if (loading) {
return <p>Loading data...</p>;
}
return (
<div>
<center>
<h1 style={{ color: 'green' }}>
GeeksforGeeks
</h1>
<h3>
How to avoid infinite loop when
using useEffect hook in React
</h3>
</center>
<h3>Posts</h3>
<ul>
{data.map((post) => (
<li key={post.id}>
<h2>{post.title}</h2>
<p>{post.body}</p>
</li>
))}
</ul>
</div>
);
}
export default App;
To start the application run the following command.
npm start
Output: This output will be visible on the http://localhost:3000 on the browser window.
.gif)
Avoid Infinite Loops When Using useEffect() in ReactJS
Approach 2: Using Memoization
In this example, we are using the useCallback hook to memoize the handleClick function. This function is passed down to the ChildComponent as a prop, so we want to ensure that it is only re-created when its dependencies change. We pass an empty array as the dependency array, which means that the function is only created once.
Example: Below example demonstrates how to avoid infinite loops using memoization in the useEffect hook in React.
JavaScript
// Filename - App.js
import React, {
useState,
useEffect,
useCallback
} from 'react';
function ChildComponent({ onClick }) {
return <button onClick={onClick}>Click me</button>;
}
function App() {
const [count, setCount] = useState(0);
const handleClick = useCallback(() => {
console.log('Clicked!');
}, []); // This is the dependency array
useEffect(() => {
console.log('Count changed!');
}, [count]);
return (
<div>
<center>
<h1 style={{ color: 'green' }}>
GeeksforGeeks
</h1>
<h3>
How to avoid infinite loop when
using useEffect hook in React
</h3>
</center>
<h1>
Count: {count}
</h1>
<ChildComponent onClick={handleClick} />
<button onClick={() => setCount(count + 1)}>
Increment count
</button>
</div>
);
}
export default App;
To start the application run the following command.
npm start
Output: This output will be visible on the http://localhost:3000 on the browser window.

Avoid Infinite Loops When Using useEffect() in ReactJS
Similar Reads
How to Avoid Infinite Loops When using useEffect() in ReactJS ?
useEffect() can lead to infinite loops, causing performance issues or crashes if not used correctly. In this article, we will explain what causes these infinite loops and how to avoid them when working with useEffect() in ReactJS. Avoid infinite loops in useEffect() by providing appropriate dependen
4 min read
Build an Image Search App with Infinite Scroll using React JS
This article delves into building a React-based image search app with infinite scroll. Users can search for images based on a query, and as they scroll down, additional images are fetched and shown. The Unsplash API is employed for searching and retrieving these images, resulting in a fully operatio
3 min read
Difference Between useState and useEffect Hook in ReactJS
ReactJS is the powerful JavaScript library used for building user interfaces especially single-page applications(SPAs). Two of the most commonly used hooks in React are useState and useEffect. These hooks make functional components more powerful by allowing them to manage the state and handle side e
3 min read
Handling Error using React useState and useEffect Hooks
In React, handling error states is very important to provide a smooth user experience and handle unexpected errors that may occur during data fetching or asynchronous operations. By using the useState and useEffect hooks, you can easily manage error states in your React applications. In this article
3 min read
How to change list items in ReactJS when an item is clicked ?
In ReactJS, changing items in the list when an item of the list is clicked can be done by triggering the event onClick() on the item that is currently clicked. For, reflecting the change, we also have to maintain the state in react so that after the change when the page is rendered again the changes
3 min read
How useEffect works in ReactJS ?
The useEffect hook in React is used to handle the side effects in the functional components. It allows us to perform operations like data fetching, subscriptions, and manual DOM updates. It runs on every re-render which can be controlled using the dependency array. Syntax:useEffect(() => { // Sid
3 min read
How to use useCounter hook in ReactJS ?
The useCounter is a custom hook provided by the Rooks package for React. It is a counter hook that helps build a simple counter easily and quickly. Syntax: useCounter( initialValue )Parameters: initialValue: It is of number type that describes the initial value.Return Value: counter: It is of type o
2 min read
How To Call Loading Function With React useEffect?
The useEffect runs by default after every render of the component. When placing useEffect in our component we tell React that we want to run the callback as an effect. React will run the effect after rendering and after performing the DOM updates. If we pass only a callback, the callback will run a
2 min read
How to wait for a ReactJS component to finish updating ?
To wait for a ReactJS component to finish updating, we use a loading state in our react application by use of the conditional rendering of the component. ApproachThis can be achieved by the use of the useState and useEffect hooks in the functional components. With the help of the state, we make sure
3 min read
Difference Between useEffect and useLayoutEffect Hook in ReactJS
In this article, we will learn about the differences between useEffect and useLayoutEffect in React with the help of an example. We will create a counter and implement it using both useEffect and useLayoutEffect to understand the differences practically. useEffectThe useEffect is used to perform sid
5 min read