React Hooks Flashcards

1
Q

Why or when do we need to use useRef ?

A

if you need to interact with the actual DOM (as opposed to the React virtualization of it) directly. This is pretty rare and really only when you need to derive measurements from the DOM (like width) or you’re using an external library and it needs a real DOM node to interact with.

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

What is the purpose of React.Memo ?

A

React.memo tells React “as long as the parameters being passed into this component don’t change, do not re-render it ever.

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

When would you need to use useReducer ?

A

if you have complex state updates or if you have a situation like this: all of the state updates are very similar so it makes sense to contain all of them in one function.

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

Name two hooks which are used for performance optimizations

A

useMemo and useCallback

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

Use them only when you already have a performance problem instead of pre-emptively. It adds unnecessary complexity otherwise. Which two react hooks are we talking about here ?

A

useMemo and useCallback

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

Complete the sentence —- Typically whenever React detects a change higher-up in an app, ….

A

it re-renders everything underneath it.

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

What is the purpose of React.PureComponent ?

A

a component will do a simple check on its props to see if they’ve changed and if not it will not re-render this component (or its children, which can bite you.)

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

Difference between React.PureComponent and React.Memo /useMemo hook ?

A

React.memo provides this functionality ( React.PureComponent check) for function components.

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

What is the difference between useEffect and useLayoutEffect ?

A

useLayoutEffect is almost the same as useEffect except that it’s synchronous to render as opposed to scheduled like useEffect is. useLayoutEffect runs at the same time as componentDidMount and componentDidUpdate whereas useEffect is scheduled after.

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

When should we use useLayoutEffect hook ?

A

to measure DOM nodes for things like animations

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

What is the purpose of useId hook ?

A

when you need unique identifiers to associate two objects together. An example of this would be making sure a label and an input are associated together by the htmlFor attribute.

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