Hey there, fellow devs! I’m a supplier of Hooks, and I’ve been getting a lot of questions lately about the useEffect hook. So, I thought I’d take a moment to break it down and explain what its purpose is. Hooks

First off, if you’re new to React, Hooks are a relatively new addition to the library. They were introduced in React 16.8, and they allow you to use state and other React features without writing a class. useEffect is one of the most commonly used Hooks, and it’s super powerful.
So, what’s the deal with useEffect? Well, in a nutshell, it’s a way to perform side effects in functional components. Side effects are things like data fetching, subscriptions, or manually changing the DOM. In class components, these side effects are usually handled in the componentDidMount, componentDidUpdate, and componentWillUnmount lifecycle methods. With useEffect, you can handle all of these in one place.
Let’s start with the basics. Here’s a simple example of how you might use useEffect to perform a side effect:
import React, { useEffect } from 'react';
function Example() {
useEffect(() => {
// This is where you put your side effect code
console.log('Component mounted or updated!');
// You can also return a cleanup function if needed
return () => {
console.log('Component will unmount!');
};
});
return <div>Hello, World!</div>;
}
In this example, the useEffect hook takes a function as an argument. This function is called the effect function, and it contains the code for your side effect. The effect function is called after every render, both on the initial render and on subsequent updates.
The optional return value of the effect function is a cleanup function. This cleanup function is called before the component unmounts, and it’s used to clean up any resources that were created in the effect function. In our example, we’re just logging a message to the console, but in a real-world scenario, you might use the cleanup function to unsubscribe from a subscription or clear a timer.
Now, let’s talk about when the effect function is called. By default, the effect function is called after every render. This means that if your component re-renders because of a state change or a prop change, the effect function will be called again. Sometimes, this is what you want, but other times, you might want to limit when the effect function is called.
That’s where the second argument to useEffect comes in. This argument is an array of values, and it’s called the dependency array. If you pass a dependency array as the second argument to useEffect, the effect function will only be called if one of the values in the dependency array has changed since the last render.
Here’s an example:
import React, { useState, useEffect } from 'react';
function Example() {
const [count, setCount] = useState(0);
useEffect(() => {
document.title = `You clicked ${count} times`;
}, [count]);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
In this example, we’re using useEffect to update the document title whenever the count state variable changes. We pass [count] as the second argument to useEffect, which means that the effect function will only be called if count has changed since the last render.
If you pass an empty dependency array ([]) to useEffect, the effect function will only be called on the initial render. This is equivalent to the componentDidMount lifecycle method in class components.
import React, { useEffect } from 'react';
function Example() {
useEffect(() => {
console.log('Component mounted!');
return () => {
console.log('Component will unmount!');
};
}, []);
return <div>Hello, World!</div>;
}
In this example, the effect function is only called on the initial render, and the cleanup function is called before the component unmounts.
Now, let’s talk about some real-world use cases for useEffect. One common use case is data fetching. When you’re building a web application, you often need to fetch data from an API. You can use useEffect to perform this data fetching operation.
Here’s an example:
import React, { useState, useEffect } from 'react';
function Example() {
const [data, setData] = useState(null);
const [loading, setLoading] = useState(true);
useEffect(() => {
async function fetchData() {
try {
const response = await fetch('https://api.example.com/data');
const json = await response.json();
setData(json);
setLoading(false);
} catch (error) {
console.error('Error fetching data:', error);
setLoading(false);
}
}
fetchData();
}, []);
if (loading) {
return <div>Loading...</div>;
}
return (
<div>
{JSON.stringify(data)}
</div>
);
}
In this example, we’re using useEffect to fetch data from an API on the initial render. We set the loading state variable to true initially, and we update it to false once the data has been fetched. We also handle any errors that occur during the data fetching process.
Another common use case for useEffect is subscriptions. For example, if you’re building a real-time application, you might need to subscribe to a WebSocket or an event stream. You can use useEffect to manage these subscriptions.
import React, { useEffect } from 'react';
function Example() {
useEffect(() => {
const socket = new WebSocket('ws://example.com/socket');
socket.addEventListener('message', (event) => {
console.log('Received message:', event.data);
});
return () => {
socket.close();
};
}, []);
return <div>Real-time application</div>;
}
In this example, we’re using useEffect to create a WebSocket connection on the initial render. We add an event listener to the WebSocket to handle incoming messages, and we return a cleanup function to close the WebSocket connection before the component unmounts.
So, there you have it! That’s a basic overview of the purpose of the useEffect hook. It’s a powerful tool that allows you to perform side effects in functional components in a clean and concise way. Whether you’re fetching data, managing subscriptions, or manually changing the DOM, useEffect has got you covered.

If you’re looking to integrate high – quality React Hooks into your projects, I’m here as a Hooks supplier to offer you reliable resources. Our Hooks are designed to simplify your development process and help you build more efficient and maintainable applications. If you’re interested in procurement or have any questions about our offerings, don’t hesitate to reach out for a chat. Let’s work together to take your React applications to the next level!
Hooks References:
- React official documentation
- React Hooks in Action by Michael Chan
Good Success Corp.
Good Success Corp. is one of the leading hooks manufacturers and suppliers in China. We warmly welcome you to buy cheap hooks for sale here from our factory. All customized products are with high quality and competitive price. Contact us for more details.
Address: NO.54, CHANG MA ROAD, CHANG HUA 500051, TAIWAN, R.O.C.
E-mail: sales@gscbelt.com
WebSite: https://www.gscseatbelt.com/