useRef는 React의 훅 중 하나로, 주로 두 가지 용도로 사용됨.
<input> 요소에 포커스를 주거나, 값을 가져오는 데 유용함.const inputRef = useRef(null);
const focusInput = () => {
inputRef.current.focus(); // input에 포커스
};
return (
<>
<input ref={inputRef} />
<button onClick={focusInput}>포커스 주기</button>
</>
);
const countRef = useRef(0);
const incrementCount = () => {
countRef.current += 1;
console.log(countRef.current); // 값 변경되어도 렌더링 X
};
return <button onClick={incrementCount}>클릭</button>;