Understand useEffect() Hook

Nandhu_writes
3 min readMay 27, 2024

--

useEffect is a fundamental Hook in React that allows you to perform side effects in function components. While its basic usage is straightforward—run some code after the component renders or when certain dependencies change—there are more advanced patterns and use cases for useEffect. Here are some advanced usage scenarios:

  1. Conditional Execution: You can conditionally execute the effect based on certain conditions by placing the condition inside the useEffect hook.
useEffect(() => {
if (condition) {
// Effect code here
}
}, [dependency]);

2. Cleanup: You can return a cleanup function from the effect to perform any necessary cleanup, such as unsubscribing from event listeners or cancelling network requests.

useEffect(() => {
const subscription = someObservable.subscribe(...);

return () => {
subscription.unsubscribe();
};
}, [dependency]);

3. Dependencies: It’s crucial to specify all dependencies that the effect relies on. However, sometimes you may want to skip the effect if certain dependencies have not changed. You can achieve this by storing the previous dependencies in a ref.

const prevDependencyRef = useRef(dependency);
useEffect(() => {
if (prevDependencyRef.current !== dependency) {
// Effect code here
}
prevDependencyRef.current = dependency;
}, [dependency]);

4. Debouncing and Throttling: You can use useEffect in conjunction with setTimeout or debounce and throttle utilities to control how often the effect runs, which can be helpful for performance optimization.

useEffect(() => {
const handler = setTimeout(() => {
// Effect code here
}, delay);

return () => {
clearTimeout(handler);
};
}, [dependency]);

5. Combining Multiple Effects: You can use multiple useEffect hooks to separate concerns and logically group related code.

useEffect(() => {
// Effect 1 code here
}, [dependency1]);

useEffect(() => {
// Effect 2 code here
}, [dependency2]);

6. Interaction with Outside World: You can use useEffect to interact with external libraries, APIs, or the browser's APIs safely.

useEffect(() => {
const fetchData = async () => {
try {
const result = await fetch(url);
const data = await result.json();
setData(data);
} catch (error) {
setError(error);
}
};

fetchData();

return () => {
// Cleanup code here (if any)
};
}, [url]);

what if there is no dependency array ?

If there is no dependency array provided in the useEffect hook, it means the effect will run after every render of the component, including the initial render. This can lead to performance issues if the effect involves heavy computations or causes unnecessary side effects.

Here are some implications and considerations when useEffect is used without a dependency array:

  1. Runs on Every Render: The effect will run after every render of the component. This can be useful for effects that don’t depend on any state or props and need to execute on each render.
  2. Potential Performance Issues: If the effect involves heavy computations or causes side effects that are not intended to happen on every render, it can lead to performance issues and unnecessary work.
  3. Potential Infinite Loop: Be cautious when using useEffect without dependencies, especially when the effect updates state or props that trigger a re-render. If not handled properly, it can result in an infinite loop of re-renders.
  4. Memory Leaks: Effects without dependencies that set up subscriptions or timers without proper cleanup can lead to memory leaks if resources are not properly released when the component unmounts.
  5. Consider Adding Dependencies: If the effect relies on variables that are defined outside the effect or if it interacts with external data sources, consider whether those variables should be added to the dependency array to ensure the effect runs only when necessary.
  6. Explicitly Document Intent: If you intentionally omit the dependency array, make sure to document your reasoning clearly in comments or documentation to help other developers understand the intent behind the code.

In summary, while omitting the dependency array can be appropriate for certain scenarios, it’s essential to understand the implications and use it judiciously to avoid performance issues and unexpected behavior.

--

--

Nandhu_writes

React Native developer | Still a learner | blogger : Tech life lessons || Buy me a coffee, and I'll write : https://www.buymeacoffee.com/infoappmakk