What are the use cases for useRef?
Accessing DOM elements like focusing an input, and storing mutable values that should persist across renders without triggering re-renders, like a timer id, a previous value, or a flag for whether the component is mounted.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in useRef in React Explained: Beyond Just Accessing the DOM
useRef returns a mutable object with a current property that persists across renders. Mutating current does not trigger a re-render, which is the key difference from useState.
useState triggers a re-render when it changes. useRef does not. Use useState for values that should update the UI; use useRef for values that should persist without updating the UI, like DOM references or timer ids.
Because useRef does not trigger re-renders. If a value should change the UI, it belongs in useState, not useRef. useRef is for values that persist without driving the UI.
Still have questions?
Browse all our FAQs or reach out to our support team
