Production & the useEffect() Hook
Tags: webdev, react, javascript, devjournal
Published: 2021-08-15 23:26:37

<h1>Production! Here we go!</h1>
When I got a job as a developer, all I wanted was a place to work and grow. In addition to that, I wasn't after the pay - needless to say - but I was ready to work for free as I only desired an environment to use previously acquired knowledge, and build amazing stuff. Call me naive (for not looking for money), but I have what I wanted, and a whole lot more😉🤭. 

Last week Friday, the 13th of August, will forever be etched in my memory; a product my team and I have worked on in the past weeks moved to `production(prod)`. This launched us into a brand new world of far greater responsibility and decision-making.

<h2>What is `production` and what does it entail?</h2>
There are different environments in the web development world. The first is called `development`: an environment where features are built and tested. The environment where such features are deployed to active customers for their usage is called `production`.

With new experiences come challenges, and moving to `production` isn't left out: With our app having users, it demands extra carefulness before deploying new features, but hey! we're up for the ride. In the end, I am most grateful for having found a sense of belonging in software engineering, and by the grace of my Lord Jesus, my all will be on display.
<br>

<h1>Last week on my learning journey</h1>

Last week, I added a new tool to my kit: the `useEffect()` hook. 

<figure>
<img src="https://images.unsplash.com/photo-1591267990532-e5bdb1b0ceb8?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1313&q=80" />
<figcaption>Photo by <a href="https://unsplash.com/@flowforfrank?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Ferenc Almasi</a> on <a href="https://unsplash.com/s/photos/useeffect?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a></figcaption>
</figure>
<br>


  

React is built on the idea of components, and styling them can be hellish at times. Some cannot be styled by simply passing CSS on them, probably because they are from an API you're using(e.g. <a href="https://getstream.io">getStream</a>) or have styles that are difficult to control in the environment they're being staged. Situations like this present a unique headache, one that requires a unique solution.

Thankfully, React has a powerful `useEffect()` hook that as its name, is used to make effect-related changes in your code. Although it must be said that inappropriate usage can cause a memory leak on the browser, causing the webpage that houses the hook to break. Below, I discuss what `useEffect()` is,  what it does, and how it is used.

<h2>1. useEffect() is just as its name, for side-effects.</h2>

React components are structured to take `props` and `state` to render, depending on how the component is to be used. Although sufficient, components in React are built to be reusable, meaning some extra details might need to be modified where they are imported. An example of such modification includes tapping into the DOM(document object model), a process not directly accounted for in JSX. In such a case, a hook is necessary. step up `useEffect()`.

How components render in React is a very tricky subject, and by my estimation, it would take a lifetime to fully get a hang of it, as use cases differ by the day. Although when React cannot be stopped when it decides to render a component, desired effects(or behaviors) can be introduced into such components using the useEffect() hook. These effects can be further decoupled to exist independently of the parent component, and their rendering behaviors structured as desired.

<h2>2. How is it used?</h2>

```javascript
import { useEffect } from 'react';
```


A useEffect() hook is built to accept two(2) arguments: a function called "Effects" and an optional array of dependencies.

`useEffect(function, [dependencies(either specific or not)]);`

UseEffect () executes the first argument(function) after React has committed the component to the screen. This logically means one thing, that the useEffect() hook can work without a second argument. Although true, it comes with a bad experience, which takes me into a further explanation about the second argument, the optional dependencies.

Dependency is an optional array of dependencies. They are optional because they are either specific or not, i.e., they can target a particular thing or be left empty. 

I consider different case-scenarios below:

<h2>CASE 1</h2>
```javascript
useEffect(() => {
Some DOM manipulation
    });
}
```

Developer: run the DOM manipulation.
React: Okay, but how many times?
Developer: just run the code.
React: Okay. *causes a re-render of the component until the browser can no longer take it, causing a memory leak and eventual crash of the page*
Why this behavior?  React keeps putting the effect into use over and over again, even without the page re-rendering.

<h2>CASE 2</h2>
```javascript
useEffect(() => {
Some DOM manipulation
    }, []);
}
```

Developer: run the DOM manipulation.
React: Okay, but how many times?
Developer: only when the component itself re-renders.
React: Okay. *renders the effect once unless there is a change in the component; a component serenader causes it to also re-render* 

<h2>CASE 3</h2>
```javascript
useEffect(() => {
Some DOM manipulation
    }, [xxx]);
}
```

Developer: run the DOM manipulation.
React: Okay, but how many times?
Developer: once on page load, and listen to the dependency for changes. If any, re-render the effect.
React: Okay. *renders the effect on loading of the page, listens to the dependency for changes, and if any, re-renders.* 


<h2>CASE 4</h2>
```javascript
useEffect(() => {
Some DOM manipulation
    }, [xxx, yyy]);
}
```

Developer: run the DOM manipulation.
React: Okay, but how many times?
Developer: once on page load, and listen to the two dependencies for changes. If any in either, re-render the effect.
React: Okay. *renders the effect on loading the page, listens to both dependencies for changes, and if any, re-renders.*

This last example also gives an insight: the `useEffect()` hook can take either zero or more than one dependency. How they(dependencies) are structured depends on the desired output of the effect.

N.B: `useEffect()` executes the first argument(function) only if the dependencies have changed between renderings.

Would you like to read more on the useEffect() hook, I gladly recommend these two posts titled "<a href="https://dev.to/trunghieu99tt/you-don-t-know-useeffect-4j9h">You don't know useEffect</a>" and "<a href="https://dmitripavlutin.com/react-useeffect-explanation/">React useEffect explanation</a>".