What is the primary purpose of Refs in React, as described in the text?
To get access to actual DOM elements when necessary, bypassing React’s abstraction.
List two typical use cases for accessing the native DOM API in React.
Manually focusing an element after it’s rendered or calculating the size and boundaries of a component.
Instead of using getElementById, what more powerful way does React provide to access DOM elements without spreading ids everywhere?
React provides Refs to access DOM elements in a more integrated way.
In React, what is a Ref?
A mutable object that React preserves between component re-renders.
Which React hook is used to create a Ref?
The useRef hook.
After creating a Ref with const ref = useRef(initialValue), how do you access the value it holds?
You access the value through the ref.current property.
What is the most significant difference between updating a Ref and updating state?
Updating a Ref’s .current property does not trigger a component re-render, whereas updating state does.
Why can’t you use a Ref to display a character count for an input field that updates as the user types?
Because a Ref update doesn’t cause a re-render, the UI will not be updated to show the new count.
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?
The displayed value will suddenly update to the latest value stored in the Ref.
What is the second major difference between Ref updates and state updates regarding their execution timing?
Ref updates are synchronous and immediate, while state updates are typically asynchronous and batched by React.
In an onChange handler, if you log a state variable immediately after calling its setValue function, what value will you see?
You will see the old state value, because state updates are scheduled and not immediate.
In an onChange handler, if you log ref.current immediately after assigning a new value to it, what value will you see?
You will see the new, just-assigned value, because Ref mutation is a synchronous operation.
Under what two conditions is it acceptable to use a Ref to store a value instead of state?
When the value is not used for rendering and is not passed as props to other components.
What is a common use case for a Ref that doesn’t involve the DOM, such as tracking component information?
Counting how many times a component renders or storing the previous value of a prop or state.
How do you assign a Ref created with useRef to a specific DOM element in JSX?
You pass the Ref object to the element’s ref attribute, like <input ref={myRef} />.
In the component lifecycle, when does React assign the DOM element to the ref.current property?
The ref.current property is assigned only after the component has rendered and its corresponding DOM element has been created.
Where in a function component should you safely access or modify ref.current to ensure the DOM element exists?
You should only access or modify ref.current within a useEffect hook or in event handler callbacks.
If a parent component needs to call .focus() on an input inside a child component, what is the recommended approach using Refs?
Create the Ref in the parent, pass it down to the child, and attach it to the input element there.
What happens if you try to pass a Ref to a functional component using the standard ref prop, like <MyComponent ref={myRef} />?
You will get a warning in the console, as functional components cannot be given refs directly this way.
To allow a functional component to receive a Ref via the ref attribute, you must wrap that component in the _____ function.
forwardRef
When a component is wrapped in forwardRef, how does it receive the forwarded ref?
The ref is passed as the second argument to the component function, right after props.
The hook that allows you to customize the instance value that is exposed to parent components when using ref is called _____.
useImperativeHandle
What is the purpose of creating an imperative API for a component?
To abstract away internal implementation details and expose specific functions (like .focus() or .shake()) for a parent to call.
What are the three arguments passed to the useImperativeHandle hook?
The ref to be customized, a function that returns the API object, and a dependency array.