useEffect Hooks in React
Published: 2023-10-20 07:38:52

_**Unlocking the Secrets of useEffect in React: **_
**_My attempt at simplifying useEffect:_**
Imagine you have a smart house, or a bunch of smart appliances. Various activities happen automatically—lights turn off and on, the temperature adjusts, and the doors lock or unlock based on different factors like time or whether someone is home. In React, a tool used to build websites, useEffect is like the system that manages these automated activities, known as "side effects."

_**What is useEffect?**_
useEffect is a tool that helps us manage various activities in our website automatically. Just like a smart home system that manages lights or temperature, useEffect takes care of different tasks such as loading data or reacting to user interactions.
useEffect performs side effects in a function component. Side effects could be data-fetching, subscriptions, manually changing the DOM, etc.

_**When Do We Use useEffect?**_
In a smart house, you might want the lights to turn on automatically at 6 PM. Similarly, in a website, you might want to load some data as soon as a visitor arrives.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qrgfi2tvdi7p4h8gyur8.png)




_**Cleaning Up With useEffect**_
After hosting a party, you’d clean up and reset your home. Similarly, useEffect lets us clean up after activities, ensuring that everything runs smoothly and nothing is left hanging.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ntcop67djcznxs8szmja.png)


_**Custom Rules with useEffect**_
Sometimes, you might have specific rules, like turning off lights in unoccupied rooms. You can customize useEffect to handle such special cases, making it adaptable to various needs.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/75y0d7yt50jzhp3y36i8.png)


**_Guidelines for Using useEffect_**
Avoid Endless Loops: Make sure your commands don’t keep repeating endlessly, like a light that keeps turning off and on.

Specify Dependencies: Ensure you clearly state what factors or conditions your activities depend on, so they run at the right times.

_**Advanced Automation with useEffect: **_
Tailoring Your Smart House
Continuing with our smart house analogy, let’s delve deeper into the customization and advanced features that useEffect offers in enhancing our React homes (websites).

_**Multiple useEffect for Different Activities**_
Imagine you want to control the lights in one room and the air conditioning in another separately. In React, you can use multiple useEffect hooks to manage different activities independently.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m3gm0vkehkyh242jrtkm.png)


Here, each useEffect is like a separate control system managing a different part of the house, allowing for more specialized and organized configurations.

_**Crafting Custom Controls with useEffect**_
Custom hooks in React are like creating your personalized smart home routines or modes, like a "Cinema Mode" that dims the lights and sets the room temperature to a comfortable level.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xgay2oz4gqev75mpbj9d.png)


This way, you can create reusable sets of commands (routines) that can be easily applied whenever needed, making the system more versatile and user-friendly.

**_Conclusion_**
Understanding useEffect is like mastering the control system of a smart house in the world of website development. It allows your website to perform various tasks automatically, making the user’s experience smooth and dynamic. With useEffect, you can manage, customize, and optimize these tasks to create effective and user-friendly websites.

more info https://legacy.reactjs.org/docs/hooks-effect.html