An explanation of the useRef hook in React.
Join DevzConnect — where devs connect, code, and level up together. Got questions? Stuck on a bug? Or just wanna help others crush it? Jump in and be part of a community that gets it
Welcome back to DevzConnect — where devs connect, code, and level up together. Ready to pick up where you left off? Dive back in, ask questions, share wins, or help others crush their goals!
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The
useRefhook in React is used to persist values across renders without causing a re-render. It can store a reference to a DOM element, a value, or even a function that doesn’t change between renders. It’s useful for accessing or modifying a DOM element directly or for keeping track of values that should not trigger a re-render when they change.Syntax:
const myRef = useRef(initialValue);
initialValue: The initial value you want to store in theuseRefobject (optional). For DOM elements, it’s usuallynull, and for general values, it can be any value like0, an empty string, or an object.myRef.current: Thecurrentproperty of the object returned byuseRefwill store the actual value you’re referring to.Key Uses of
useRef:useRefto reference and interact with a DOM element directly without needing to usedocument.querySelector()or other DOM methods.useRefthat doesn’t change between renders and can be accessed later.Example 1: Accessing DOM Elements with
useRefIn this example, we’ll focus on using
useRefto access a DOM element (like an input field) directly and manipulate it.Explanation:
inputRefis initialized usinguseRef(null)because it’s going to store a reference to the DOM element (the input field).inputRefto therefattribute of the<input>element.handleFocusfunction accessesinputRef.current(the DOM element) and callsfocus()on it, causing the input field to gain focus.Example 2: Storing Mutable Values Without Causing Re-Renders
Here’s how you can use
useRefto persist a value without triggering a re-render:Explanation:
prevCountRefstores the previous count value usinguseRef().useEffect, we updateprevCountRef.currentto the currentcountwhenever it changes.prevCountRef.currentdoesn’t trigger a re-render, so the UI will still reflect the latest count but will show the previous value from theuseRefobject.Key Points About
useRef:Doesn’t trigger re-renders: Updating the
.currentproperty of a ref does not cause the component to re-render. This is why it’s useful for storing mutable values that do not need to affect the UI directly.DOM references: When you pass a
refto a DOM element,useRefwill hold a reference to that element, allowing you to directly interact with the DOM.Persisted values: You can store values (like timers, previous states, etc.) in
useRefthat persist across renders but don’t cause re-renders when updated.Example 3: Storing a Timer ID
Here’s an example where
useRefis used to store a timer ID so that you can clear it when needed:Explanation:
timerRefstores the ID of the timer returned bysetInterval.useEffectto start the timer when the component mounts and clean it up when the component unmounts (to avoid memory leaks).setStateonly to update thesecondsvalue.Conclusion:
The
useRefhook is a versatile tool in React for managing references to DOM elements, storing mutable values across renders, and preventing unnecessary re-renders. It is essential for cases where you need to directly manipulate the DOM, store previous values, or hold on to values that don’t need to trigger a UI update when changed.