React useEffect Flashcards

1
Q

Give three examples of effects.

A

What are effects, really? Examples are:

Fetching data
Reading from local storage
Registering and deregistering event listeners

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

How should you write asynchronous code in react?

A

Using useEffect( )

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

Is it a good practice to use more than one useEffect in a component ?

A

It is, don’t be afraid to use multiple useEffect statements in your component. While useEffect is designed to handle only one concern, you’ll sometimes need more than one effect.

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

Is useEffect designed to handle only one concern?

A

Yes useEffect is designed to handle only one concern. If you have more than one concern you need to add more useEffects.

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

What are the problems of using only one effect for multiple purposes?

A

It decreases the readability of your code, and some use cases are straight-up not realisable.

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

Should you think about hooks in terms of lifecycle methods?

A

No, don’t think in lifecycle methods anymore!

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

When are effects executed?

A

After render. To be more specific, they execute both after the first render and after every update.

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

Can effects block the UI?

A

No effects don’t block the UI because they run asynchronously.

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

What determines whether an effect will be executed?

A

Other than always being ran after first render, the dependency array determines whether an effect will be executed. If a dependency array does not exist then an effect will always be executed. If a dependency array does exist it will only run an effect if a change has been made to the dependency array.

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

What are acceptable Array values for useEffect dependency arrays and where must they come from?

A

Dependency array values must be from the component scope (i.e., props, state, context, or values derived from the aforementioned).

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

Changes to what three things can cause a component to be re-rendered.

A

Changes to state, prop, or context cause components to be re-rendered.

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

What are the “conditions” to execute for a useEffect call?

A

In the case of useEffect the “conditions” to execute are whether a useEffect dependency array exists or not and if it does have any values changed since the last render cycle.

If no dependency array is present an effect is executed after every re-render.

If a dependency array exists an effect is only executed if the dependency array’s values have changed.

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

Does a useEffect always get called on a components initial render?

A

Yes it does, it runs after first render and then after every update (assuming if a dependency array exists, it has changed).

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

What do the hooks useState, useEffect and useRef give you access too? (this is a stupid question for my own understanding)

A

useState lets you use state,
useEffect lets you use effects,
useRef lets you use refs

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

Does the order of your effects matter?

A

Yes they are executed in the order in which they are declared in the code.

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

How do you make an effect execute only once?

A

Add an empty dependency array. Effects are only executed once;

17
Q

What causes stale state ?

A

Accessing state before the component has re-rendered.

When you change state using a state setting function the component re-renders due to state changing. If you try and access state straight away inside the state setting function you will get the previous value because the component hasn’t re-rendered yet. When the component re-renders the state variable will be up to date.