← 문제 목록
고급 React 100P

forwardRef와 useImperativeHandle 오용

부모에서 inputRef.current.focus()를 호출하면 에러가 발생합니다. 버그를 찾으세요:
JSX
function CustomInput({ ref }) {
  const localRef = useRef(null);

  useImperativeHandle(ref, () => ({
    focus: () => localRef.current.focus(),
    clear: () => { localRef.current.value = ''; },
  }));

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

function Form() {
  const inputRef = useRef(null);

  return (
    <div>
      <CustomInput ref={inputRef} />
      <button onClick={() => inputRef.current.focus()}>포커스</button>
    </div>
  );
}

답변

로그인 후 제출하기
0명 풀이 · 정답률 0%