<Input/>
커스텀 컴포넌트에 register
를 적용했는데, 작동이 되지 않았다.register
는 Uncontrolled
방식을 사용하는데, <Input/>
컴포넌트안에서 input에 focus를 주기위해 ref를 선언하였는데, ref가 덮어씌워지는 문제가 있었다.type TInputProps = {
label: Path<TSignUpProps>;
register: UseFormRegister<TSignUpProps>;
} & React.InputHTMLAttributes<HTMLInputElement>;
const Input = (props: TInputProps) => {
const inputRef = React.useRef<HTMLInputElement>(null);
const onclick = () => {
inputRef.current?.focus();
};
return (
<div
className="flex flex-col bg-primary-100 p-3 cursor-text
"
onClick={onclick}
>
<label className="cursor-text" htmlFor={props.label}>
{props.label.toUpperCase()}
</label>
<input
{...props.register(props.label)}
ref={inputRef} // <- 이 부분이 문제가 됨
placeholder={props.placeholder}
className="bg-primary-100 outline-none"
/>
</div>
);
};
export default Input;
"사용자의 입력을 기반으로 자신의 state를 관리하고 업데이트합니다."
ref
를 통해 접근하는데, 이는 기존의 vanilla JS와 유사하다. (직접 DOM에 접근하는 방식)ref.current.value
를 통해 값을 가져올 수 있다.