What is the primary use case of useRef in React?
useRef
Which of the following statements about useRef is true?
useRef creates a mutable object that persists across renders.
useRef triggers a re-render when its current value changes.
useRef is part of the React Context API.
You can use useRef to directly manipulate DOM elements.
What will happen if you update the .current property of a useRef object?
.current
How can you use useRef to track the previous value of a prop or state?
By creating a ref and using useEffect to update it after each render.
useEffect
By passing the ref as a dependency to useEffect.
By using useRef to directly store the value inside .current on each render.
Consider the following component:
import React, { useRef, useEffect } from "react"; const DomRefDemo = () => { const inputRef = useRef(null); useEffect(() => { // Focus the input field on component mount inputRef.current.focus(); }, []); return ( <div> <h1>useRef Example: DOM Access</h1> <input ref={inputRef} type="text" placeholder="I will be focused on load" /> </div> ); }; export default DomRefDemo;
In the DomRefDemo component, the input field automatically gains focus when the component loads. What would happen if you Removed the useEffect hook entirely from the code?
DomRefDemo
The input field would not focus on load because useRef alone doesn’t trigger DOM actions.
The inputRef.current value would become undefined, causing an error.
inputRef.current
The input field would still focus on load because useRef handles it automatically.
The component would fail to render because useEffect is required for useRef to work.
import React, { useRef, useState } from "react"; const MutableRefDemo = () => { const renderCount = useRef(1); const [count, setCount] = useState(0); // Increment renderCount on each re-render renderCount.current += 1; return ( <div> <h1>useRef Example: Mutable Values</h1> <p>Component Re-rendered: {renderCount.current} times</p> <p>State Count: {count}</p> <button onClick={() => setCount(count + 1)}>Increment Count</button> </div> ); }; export default MutableRefDemo;
Imagine you’re building a counter app based on the MutableRefDemo example. You want to display a message "Rendered more than 5 times!" when renderCount.current exceeds 5. Where should you check this condition?
MutableRefDemo
renderCount.current
Inside a useState setter function to update a message state.
useState
Before the return statement, assigning it to a variable using renderCount.current.
return
Directly in the JSX, using {renderCount.current > 5 ? "Rendered more than 5 times!" : ""}.
{renderCount.current > 5 ? "Rendered more than 5 times!" : ""}
Inside the useEffect hook, checking renderCount.current.
import React, { useEffect, useRef, useState } from "react"; const PreviousValueDemo = () => { const [count, setCount] = useState(0); const prevCount = useRef(count); useEffect(() => { prevCount.current = count; // Update the ref value after render }, [count]); return ( <div> <h1>useRef Example: Preserve Previous State</h1> <p>Current Count: {count}</p> <p>Previous Count: {prevCount.current}</p> <button onClick={() => setCount(count + 1)}>Increment</button> </div> ); }; export default PreviousValueDemo;
In the PreviousValueDemo component, the prevCount ref tracks the previous value of count. If count starts at 0 and the user clicks "Increment" twice, what will the JSX display for Current Count and Previous Count?
PreviousValueDemo
prevCount
count
Current Count
Previous Count
Current Count: 2, Previous Count: 0
Current Count: 2, Previous Count: 2
Current Count: 2, Previous Count: 1
Current Count: 1, Previous Count: 0
Suppose you modify the MutableRefDemo code from Question 6, to reset renderCount.current to 0 when the user clicks a new "Reset" button. What potential issue might arise?
The reset won’t work because useRef values can’t be updated.
The count state will also reset to 0 unintentionally.
The displayed render count won’t update until the component re-renders for another reason.
Resetting renderCount.current will cause an infinite re-render loop.
A developer uses useRef to track how many times a button is clicked without displaying it in the UI. Later, they decide to log this value to the console when another button is clicked. Which of these approaches would work based on the useRef behavior?
Use clickRef.current to store the DOM element of the button instead of a number.
clickRef.current
Update clickRef.current in a useEffect that depends on a state change triggered by the first button.
Store the click count in clickRef.current and update it in the first button’s onClick. Log clickRef.current in the second button’s onClick.
onClick
Store the click count in useState instead, then log it when the second button is clicked.
This content is only available to registered users.Please register at the the home page.