Using React Hooks Flashcards

1
Q

What does useEffect do in React?

A

Executes side effects in functional components.

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

What are the two parameters of the useEffect hook?

A

Effect function and an optional dependency array.

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

What happens when useEffect is used without dependencies?

A

The effect runs on every render.

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

When does useEffect run when dependencies are provided?

A

It runs when the dependencies change.

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

How do you clean up side effects in useEffect?

A

Return a cleanup function from the effect.

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

What is the purpose of the useEffect hook in data fetching?

A

To trigger side effects like fetching data after the initial render.

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

What are the rules for using React hooks?

A

Call hooks at the top level, not conditionally, and only in function components.

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

What is the purpose of useReducer?

A

It is an alternative to useState for managing more complex state logic.

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

What does useReducer return?

A

A tuple: current state and dispatch function.

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

How does a reducer function work in React?

A

It describes state changes based on actions.

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

What should you avoid when updating state with useReducer?

A

Mutating the state directly. Always return a new state object.

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

How do you reference HTML elements imperatively in React?

A

Use useRef to create a mutable object that persists across renders.

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

What does useMemo do in React?

A

Memoizes a computed value to optimize performance by recalculating only when dependencies change.

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

How does useCallback optimize performance?

A

It memoizes functions to prevent re-creation on each render.

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

How can you prevent unnecessary re-renders of child components?

A

Use useCallback or React.memo to memoize functions and components.

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

What triggers a component re-render in React?

A

A state change triggers a re-render of the component and its children.

17
Q

What does React.memo do?

A

Memoizes a component to prevent unnecessary re-renders, improving performance.

18
Q

How does memoization help with performance optimization?

A

It avoids unnecessary re-renders, particularly in computationally expensive components.