Mid From PDF React React.js

How does useRef work and what are common use cases?

useRef creates a mutable reference that persists across renders.

✅ Syntax:

const ref = useRef(initialValue);

✅ Use cases:

  • Accessing DOM elements
  • Persisting values without causing re-renders
  • Storing previous values

✅ Example (DOM access):

const inputRef = useRef();

function focusInput() {

inputRef.current.focus();

}
return <input ref={inputRef} />;

✅ Example (storing previous state):

const prevCount = useRef();

useEffect(() => {

prevCount.current = count;

});

More from React.js Tutorial

All questions for this course
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details