Contents of React Interview Questions

Comprehensive collection of React interview questions and answers covering hooks, components, state management, and best practices.

What is useRef and how is it different from useState?

useRef is a Hook that lets you persist mutable values across renders without causing a re-render.

Key differences between useRef and useState:

AspectuseRefuseState
Causes re-render on update?❌ No✅ Yes
UsageAccessing/managing DOM elements, persisting values between rendersManaging reactive UI state
Common use casesFocus input fields, track previous props/state, store timersUI updates (e.g., toggling modals, changing text)

Example using useRef to focus an input:

import { useRef } from 'react'; function FocusInput() { const inputRef = useRef(); function handleClick() { inputRef.current.focus(); } return ( <> <input ref={inputRef} /> <button onClick={handleClick}>Focus Input</button> </> ); }