[React]Styled-components에 useRef 쓰는 법 (typescript)

이ᄏᄋ·2021년 11월 18일
0

리액트

목록 보기
1/4

🎨Styled-Components에 useRef 쓰고 싶으면 어떻게 해야될까요?

ref을 붙여야 하는 component를 forwardRef으로 만든다.

const Input = forwardRef((props:ComponentProps<any>,ref) => {
    return (
        <StyledInput ref={ref} id={props.id} name={props.name} autoComplete={"off"} maxLength={props.maxlength} onBlur={props.onBlur} onChange={props.onChange} {...props}></StyledInput>
    )
});
export default Input;

이러고

      <Input ref={emailRef} name="email" type="email" width={"200px"} auto="off" id="Email" maxlength="50" onBlur={onBlur} onChange={onChange} ></Input>

이런식으로 ref을 붙이면 된다.

profile
미쳤다.

0개의 댓글