UseRef Interview Questions
5 curated useref interview questions with detailed answers and examples.
← Browse all interview questionsUse useRef to create a mutable reference that React attaches to a DOM node after mount. Access the node via ref.current inside useEffect, not during render. Use callback refs to measure elements and forwardRef to expose a child component's DOM node to a parent.
A controlled component has its value driven by React state. An uncontrolled component lets the DOM own the value, read via a ref on submit. Controlled enables real-time validation and formatting. Uncontrolled is mandatory for file inputs and is used by React Hook Form for performance.
Use forwardRef with useImperativeHandle to expose imperative methods from a child component, or use controlled props to manage disabled state from the parent.
Controlled components let React own the input value through state. Uncontrolled components let the DOM own the value and read it via refs. Know when each is appropriate.
Implement a character counter for a textarea using a controlled input. Understand why the counter should be derived state, not stored state, and when useRef is the right tool instead.