React useEffect() wonders that you should know!
Published: 2023-06-13 12:08:21

`useEffect` is a built-in hook in React that allows you to perform side effects in your functional components. It's a fundamental hook that is used extensively in React applications. Here are some important details about `useEffect` that everybody should know:

1. Purpose: `useEffect` is used for handling side effects such as data fetching, subscriptions, or manually manipulating the DOM. It enables you to perform these operations after the component has rendered or when certain dependencies have changed.

2. Syntax: The `useEffect` hook takes two arguments: a callback function and an optional array of dependencies. The callback function contains the side effect logic, while the dependency array specifies which values the effect depends on.

   ```jsx
   useEffect(() => {
     // Side effect logic
     // ...

     // Cleanup logic (optional)
     return () => {
       // Cleanup code
       // ...
     };
   }, [dependency1, dependency2]);
   ```

3. Execution Timing: By default, `useEffect` runs after every render of the component. This means it will run on the initial render and subsequent re-renders. You can control the execution of the effect by specifying dependencies. If the dependencies array is empty, the effect will only run once, after the initial render.

4. Dependency Array: The dependency array determines when the effect should be re-evaluated. If any value in the dependency array changes between renders, the effect will be re-executed. If the dependency array is omitted, the effect will run on every render.

5. Cleanup Function: If the effect requires any cleanup, such as canceling subscriptions or removing event listeners, you can return a cleanup function from the effect's callback. The cleanup function will be invoked before the component unmounts or before the effect is re-executed. This ensures that resources are properly released.

6. Dependencies and Equality: React determines whether the dependencies have changed by performing a shallow comparison (using `Object.is`) between the current and previous values. It's important to note that if the dependencies contain objects or arrays, you should ensure they are referentially stable or use proper techniques like immutability to avoid unintentional re-renders.

7. Multiple Effects: You can use multiple `useEffect` hooks in a single component to separate concerns and keep the code organized. Each effect operates independently of others.

8. Asynchronous Effects: If you need to perform asynchronous operations within the effect, you can mark the callback function as `async` and use `await` for promises. However, you should be aware that returning a promise from an effect does not cancel the effect. You may need additional cleanup logic to handle such scenarios.

9. Warning for Missing Dependencies: If you omit dependencies in the dependency array, React will display a warning in the console. It's important to provide all the dependencies that the effect relies on to ensure correct behavior.

10. Best Practices: To write efficient and predictable effects, consider the following best practices:
   - Keep the effect's logic focused and self-contained.
   - Avoid unnecessary re-execution of the effect by providing accurate dependencies.
   - Perform any cleanup to prevent memory leaks and resource waste.
   - Understand the order in which effects are executed by considering their dependencies.

By understanding and effectively using the `useEffect` hook, you can handle various side effects in your React components while adhering to the declarative nature of React's programming model.