Using the Effect Hook Flashcards

1
Q

Describe useEffect

A

The Effect Hook, useEffect, adds the ability to perform side effects from a function component. It serves the same purpose as componentDidMount, componentDidUpdate, and componentWillUnmount in React classes, but unified into a single API.

Effects may also optionally specify how to “clean up” after them by returning a function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What happens when you call useEffect?

A

When you call useEffect, you’re telling React to run your “effect” function after flushing changes to the DOM. Effects are declared inside the component so they have access to its props and state.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

When does useEffect run?

A

By default, React runs the effects after every render — including the first render.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Multiple useEffects

A

Just like with useState, you can use more than a single effect in a component.

If you have multiple effects to run make sure you separate them out rather than having all the code in a single useEffect.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Benefits of useEffect

A

Hooks let you organize side effects in a component by what pieces are related (such as adding and removing a subscription), rather than forcing a split based on lifecycle methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are some examples of side effects?

A

Data fetching, setting up a subscription, and manually changing the DOM in React components are all examples of side effects

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How is useEffect related to the component life cycle?

A

You can think of useEffect Hook as componentDidMount, componentDidUpdate, and componentWillUnmount combined.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are two common side effect types in react?

A

There are two common kinds of side effects in React components: those that don’t require cleanup, and those that do.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Effects Without Cleanup

A

Sometimes, we want to run some additional code after React has updated the DOM. Network requests, manual DOM mutations, and logging are common examples of effects that don’t require a cleanup. We say that because we can run them and immediately forget about them.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does useEffectDo?

A

What does useEffect do?

By using this Hook, you tell React that your component needs to do something after render. React will remember the function you passed (we’ll refer to it as our “effect”), and call it later after performing the DOM updates. In this effect, we set the document title, but we could also perform data fetching or call some other imperative API.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Why is useEffect called inside a component?

A

Why is useEffect called inside a component?

Placing useEffect inside the component lets us access the state variable (or any props) right from the effect. We don’t need a special API to read it — it’s already in the function scope. Hooks embrace JavaScript closures and avoid introducing React-specific APIs where JavaScript already provides a solution.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Does useEffect run after every render?

A

Does useEffect run after every render?

Yes. By default, it runs both after the first render and after every update. (We will later talk about how to customize this.) Instead of thinking in terms of “mounting” and “updating”, you might find it easier to think that effects happen “after render”. React guarantees the DOM has been updated by the time it runs the effects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Does useEffect block the browser from updating the screen?

A

Unlike componentDidMount or componentDidUpdate, effects scheduled with useEffect don’t block the browser from updating the screen. This makes your app feel more responsive. The majority of effects don’t need to happen synchronously.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Effects with Clean up

A

Earlier, we looked at how to express side effects that don’t require any cleanup. However, some effects do. For example, we might want to set up a subscription to some external data source. In that case, it is important to clean up so that we don’t introduce a memory leak.

Code for adding and removing a subscription is so tightly related that useEffect is designed to keep it together. If your effect returns a function, React will run it when it is time to clean up.

Effects might not have a cleanup phase, so they won’t return anything.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you clean up in useEffect?

A

How do you clean up in useEffect?

This is the optional cleanup mechanism for effects. Every effect may return a function that cleans up after it. This lets us keep the logic for adding and removing subscriptions close to each other. They’re part of the same effect

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

When exactly does React clean up an effect?

A

When exactly does React clean up an effect?

React performs the cleanup when the component unmounts. However, as we learned earlier, effects run for every render and not just once. This is why React also cleans up effects from the previous render before running the effects next time.

17
Q

Does the returned function that cleans up have to be named?

A

We don’t have to return a named function from the effect. We called it cleanup here to clarify its purpose, but you could return an arrow function or call it something different.

18
Q

Use Multiple Effects to Separate Concerns

A

One of the problems we outlined in the Motivation for Hooks is that class lifecycle methods often contain unrelated logic, but related logic gets broken up into several methods.

So, how can Hooks solve this problem? Just like you can use the State Hook more than once, you can also use several effects. This lets us separate unrelated logic into different effects.

Hooks let us split the code based on what it is doing rather than a lifecycle method name. React will apply every effect used by the component, in the order they were specified.

19
Q

Why do Effects Run on Each Update?

A

There is no special code for handling updates because useEffect handles them by default. It cleans up the previous effects before applying the next effects.

This behavior ensures consistency by default and prevents bugs that are common in class components due to missing update logic.

20
Q

Conditionally run useEffect and second parameter dependency array.

A

In some cases applying the effect after every render can cause performance issues, so you need a way to conditionally run effect from a component.

To conditionally execute an effect we pass in a second parameter. This parameter is an array. Within this array you need to specify the props or state that we need to watch for. Only if those props and state specified in the array were to change the effect would be executed.

21
Q

How do you have useEffect run only once?

A

The way we do that is by specifying an empty array as the second parameter to the useEffect. We’re telling react that this effect does not depend on any props or state so there’s no reason to call this effect on re-renders. This makes react call the effect on the initial render and forget about it.