react-effects Flashcards

1
Q

When is a component “mounted” to the DOM?

A

Triggering a render (delivering the guest’s order to the kitchen)
Rendering the component (preparing the order in the kitchen)
Committing to the DOM (placing the order on the table)

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

What is a React Effect?

A

Effects let you run some code after rendering so that you can synchronize your component with some system outside of React.

Effects let you specify side effects that are caused by rendering itself, rather than by a particular event.

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

When should you use an Effect and when should you not use an Effect?

A

Keep in mind that Effects are typically used to “step out” of your React code and synchronize with some external system. This includes browser APIs, third-party widgets, network, and so on. If your Effect only adjusts some state based on other state, you might not need an Effect.

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

When do Effects run?

A

By default, useEffect runs after each render of the component where it’s called.
The effect runs when the component is mounted, and whether or not it runs on subsequent updates is determined by an array of dependencies passed

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

What function is used to declare an Effect?

A

useEffect

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

What are Effect dependencies and how do you declare them?

A

You can tell React to skip unnecessarily re-running the Effect by specifying an array of dependencies as the second argument to the useEffect call. Start by adding an empty [] array to the above example on line 14:
Specifying [isPlaying] as the dependency array tells React that it should skip re-running your Effect if isPlaying is the same as it was during the previous render.

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

Why would you want to clean up from an Effect?

A

Prevent unexpected errors, so we dont keep things running when they dont have to be or when something unmounts

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

How do you clean up from an Effect?

A

you return a function that disconnects

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

When does the cleanup function run?

A

However, it is pertinent to note that the useEffect cleanup function does not only run when our component wants to unmount, it also runs right before the execution of the next scheduled effect.

React will call your cleanup function each time before the Effect runs again, and one final time when the component unmounts (gets removed).

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