React Hooks Flashcards

useEffect (25 cards)

1
Q

What is a React hook?

A

A React hook is a special function that lets you use state and other React features in functional components.

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

True or False: Hooks can only be used in class components.

A

False

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

What hook would you use to manage local state in a functional component?

A

useState

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

Fill in the blank: The __________ hook allows you to perform side effects in functional components.

A

useEffect

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

What does the useContext hook provide access to?

A

React context

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

Multiple choice: Which of the following is NOT a built-in React hook? a) useReducer b) useMemo c) useConnect d) useRef

A

c) useConnect

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

How do you define a custom hook?

A

By creating a function that uses built-in hooks and naming it with ‘use’ prefix.

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

True or False: Hooks can be called conditionally inside components.

A

False

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

What is the purpose of the useMemo hook?

A

To memoize expensive calculations and avoid recalculating on every render.

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

What does the useReducer hook help manage?

A

Complex state logic in functional components.

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

Fill in the blank: The __________ hook can be used to access the previous state value.

A

useRef

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

Multiple choice: Which hook is used to subscribe to context? a) useState b) useContext c) useEffect d) useReducer

A

b) useContext

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

What is the return value of the useState hook?

A

An array with the current state and a function to update it.

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

True or False: You can use multiple useEffect hooks in a single component.

A

True

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

When should you use the useLayoutEffect hook?

A

When you need to perform synchronous actions after all DOM mutations.

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

What is the primary difference between useEffect and useLayoutEffect?

A

useEffect runs after the paint, while useLayoutEffect runs before the paint.

17
Q

Fill in the blank: The __________ hook is used to persist values between renders without causing a re-render.

18
Q

Multiple choice: Which hook is best for optimizing performance by memoizing a value? a) useState b) useMemo c) useEffect d) useCallback

19
Q

What does the useCallback hook do?

A

It returns a memoized version of the callback function that only changes if one of the dependencies has changed.

20
Q

True or False: Hooks can be called inside loops, conditions, or nested functions.

21
Q

What is the purpose of the useImperativeHandle hook?

A

To customize the instance value that is exposed when using ref.

22
Q

Fill in the blank: The __________ hook allows you to share logic between components.

23
Q

How do you handle cleanup in useEffect?

A

By returning a cleanup function from the effect.

24
Q

Multiple choice: Which hook would you use to perform an action after a component mounts? a) useEffect b) useMemo c) useReducer d) useState

25
What is a common pattern when using useEffect?
To specify dependencies in an array to control when the effect runs.