useEffect Flashcards

1
Q

True or False: The useEffect hook runs after every render of the component.

A

True

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

What is the primary purpose of the useEffect hook in React?

A

To perform side effects in functional components.

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

True or False: useEffect runs after every render.

A

True.

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

Fill in the blank: useEffect takes two arguments: a function and __________.

A

an array of dependencies.

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

What is the default behavior of useEffect if no dependency array is provided?

A

It runs after every render.

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

What is the purpose of the dependency array in useEffect?

A

To control when the effect runs based on changes to specified values.

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

Multiple Choice: Which of the following is a valid useEffect syntax?
A) const effect = () => useEffect( /* effect */ )

B) useEffect(() => { /* effect */ }, [dependencies]

C) const { useEffect } = () => { /* effect */ }

D) function useEffect () { /* effect */ }

A

B) useEffect(() => { /* effect */ }, [dependencies]);

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

What happens if an empty array is passed as the second argument to useEffect?

A

The effect runs only once after the initial render.

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

True or False: useEffect can be used to fetch data.

A

True.

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

What is the cleanup function in useEffect used for?

A

To clean up subscriptions or side effects when the component unmounts or before the effect runs again.

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

Fill in the blank: useEffect is called ________ when the component mounts.

A

after

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

What will happen if the dependency array contains a state variable that changes frequently?

A

The effect will run every time that state variable changes.

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

Multiple Choice: Which hook can be used in conjunction with useEffect for state management?

A) useReducer

B) useContext

C) useState

D) useRef

A

C) useState.

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

Short Answer: What is the syntax to return a cleanup function from useEffect?

A
return () => { /* cleanup code */ };
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

True or False: You can call useEffect conditionally within a component.

A

False.

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

What is a common use case for useEffect?

A

Fetching data from an API.

17
Q

Which of the following is NOT a valid use case for useEffect?
(A) Fetching data
(B) Updating the DOM directly
(C) Setting up a subscription
(D) Returning a value from a component

A

D) Returning a value from a component.