React useEffect Hook
The useEffect hook in React is used to perform side effects in functional components. Side effects can include data fetching, subscriptions, or manually changing the DOM, which are things you usually do outside of the main React rendering process.
Structure for useEffect hook
useEffect(() => { setup of the hook}, [ dependencies of the hook.]);
Optionally, you can return a cleanup function that runs before the component unmounts.
useEffect(() => { setup of the hook
return () => { you can optionally add this cleanup function};
}, [dependencies of the hook]);
Why use return() in useEffect
If the component using this effect is about to be removed from the DOM, the cleanup function runs to perform any necessary cleanup operations, like removing event listeners or canceling network requests.
If the effect is set to re-run due to changes in its dependencies, the cleanup function runs before the effect runs again. This helps prevent memory leaks and ensures that only the latest effect is active.
Example 01
import React, { useState, useEffect } from ‘react’;
function Example() {
const [count, setCount] = useState(0);
Get Ugddulmina’s stories in your inbox
Join Medium for free to get updates from this writer.
useEffect(() => {
// This code runs after every render.
document.title = `You clicked ${count} times`;
// Cleanup function
return () => {
console.log(‘Cleaning up’);
};
}, [count]); // Only re-run the effect if count changes
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>Click me</button>
</div>
);
}
export default Example;
code explanation:
In initial render, effect runs and setting the document title to ‘You clicked 0 times’. next render, when count changes(when button is clicked), the effect runs again because count is in the dependency array. Before running the new effect, React calls the cleanup function from the previous effect. This is logged as ‘Cleaning up’.The new effect then sets the document title to the updated count, e.g., ‘You clicked 1 itemes’.
Why useEffect need for this code?
1. Managing Side Effects
Changing the document title is considered a side effect because it affects something outside of the component’s scope. React’s functional components should be pure, meaning they should not cause side effects directly within the rendering process. useEffect is designed to handle such side effects in a controlled and predictable way.
2. Synchronizing with State Changes
By using useEffect, we can synchronize the document title with the count state. The useEffect hook ensures that the document title is updated whenever the count state changes. Without useEffect, this synchronization would be harder to manage and could lead to bugs or inconsistencies.
3. Dependency Management
The second argument to useEffect is the dependency array [count]. This tells React to re-run the effect only when count changes. This optimizes performance by avoiding unnecessary updates. Without useEffect, you would need to manually track changes to the count state and update the document title accordingly.
4. Cleanup Function
useEffect allows you to define a cleanup function that runs before the component unmounts or before the effect runs again. Although the provided example does not require a cleanup, it demonstrates how you can use useEffect to clean up resources like event listeners or timers. This is crucial for avoiding memory leaks and ensuring that the component behaves correctly throughout its lifecycle.







