9. React Refs Flashcards

(31 cards)

1
Q

What is the primary purpose of Refs in React, as described in the text?

A

To get access to actual DOM elements when necessary, bypassing React’s abstraction.

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

List two typical use cases for accessing the native DOM API in React.

A

Manually focusing an element after it’s rendered or calculating the size and boundaries of a component.

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

Instead of using getElementById, what more powerful way does React provide to access DOM elements without spreading ids everywhere?

A

React provides Refs to access DOM elements in a more integrated way.

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

In React, what is a Ref?

A

A mutable object that React preserves between component re-renders.

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

Which React hook is used to create a Ref?

A

The useRef hook.

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

After creating a Ref with const ref = useRef(initialValue), how do you access the value it holds?

A

You access the value through the ref.current property.

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

What is the most significant difference between updating a Ref and updating state?

A

Updating a Ref’s .current property does not trigger a component re-render, whereas updating state does.

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

Why can’t you use a Ref to display a character count for an input field that updates as the user types?

A

Because a Ref update doesn’t cause a re-render, the UI will not be updated to show the new count.

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

If a component using a Ref for a value re-renders due to an unrelated state change, what happens to the displayed value from the Ref?

A

The displayed value will suddenly update to the latest value stored in the Ref.

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

What is the second major difference between Ref updates and state updates regarding their execution timing?

A

Ref updates are synchronous and immediate, while state updates are typically asynchronous and batched by React.

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

In an onChange handler, if you log a state variable immediately after calling its setValue function, what value will you see?

A

You will see the old state value, because state updates are scheduled and not immediate.

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

In an onChange handler, if you log ref.current immediately after assigning a new value to it, what value will you see?

A

You will see the new, just-assigned value, because Ref mutation is a synchronous operation.

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

Under what two conditions is it acceptable to use a Ref to store a value instead of state?

A

When the value is not used for rendering and is not passed as props to other components.

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

What is a common use case for a Ref that doesn’t involve the DOM, such as tracking component information?

A

Counting how many times a component renders or storing the previous value of a prop or state.

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

How do you assign a Ref created with useRef to a specific DOM element in JSX?

A

You pass the Ref object to the element’s ref attribute, like <input ref={myRef} />.

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

In the component lifecycle, when does React assign the DOM element to the ref.current property?

A

The ref.current property is assigned only after the component has rendered and its corresponding DOM element has been created.

17
Q

Where in a function component should you safely access or modify ref.current to ensure the DOM element exists?

A

You should only access or modify ref.current within a useEffect hook or in event handler callbacks.

18
Q

If a parent component needs to call .focus() on an input inside a child component, what is the recommended approach using Refs?

A

Create the Ref in the parent, pass it down to the child, and attach it to the input element there.

19
Q

What happens if you try to pass a Ref to a functional component using the standard ref prop, like <MyComponent ref={myRef} />?

A

You will get a warning in the console, as functional components cannot be given refs directly this way.

20
Q

To allow a functional component to receive a Ref via the ref attribute, you must wrap that component in the _____ function.

21
Q

When a component is wrapped in forwardRef, how does it receive the forwarded ref?

A

The ref is passed as the second argument to the component function, right after props.

22
Q

The hook that allows you to customize the instance value that is exposed to parent components when using ref is called _____.

A

useImperativeHandle

23
Q

What is the purpose of creating an imperative API for a component?

A

To abstract away internal implementation details and expose specific functions (like .focus() or .shake()) for a parent to call.

24
Q

What are the three arguments passed to the useImperativeHandle hook?

A

The ref to be customized, a function that returns the API object, and a dependency array.

25
In a component using `useImperativeHandle`, the object returned by the second argument (the factory function) becomes the value of what?
It becomes the value of the forwarded `ref.current` property in the parent component.
26
What is a simpler alternative to `useImperativeHandle` for creating an imperative API on a component?
Manually assigning the API object to the passed-in `ref.current` property inside a `useEffect` hook.
27
Is using an imperative API a common pattern in React?
No, it is considered an escape hatch; the normal props/callbacks data flow is preferred in most cases.
28
Complete the code to create a Ref in a functional component: `const myRef = _____(null);`
useRef
29
Fill in the blanks to wrap a component with `forwardRef`: `const InputField = forwardRef((_____, _____) => { return ; });`
props, ref
30
In React, a Ref is a _____ object that can store any value and will be preserved between re-renders.
mutable
31
Why is it a bad practice for a parent `Form` component to have direct access to a child `InputField`'s underlying DOM element?
It leaks internal implementation details and breaks the principle of separation of concerns.