You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
종길이는 팀원들을 위해 Input 공통 컴포넌트를 만들었다.
하지만 ref를 생각하지 못해서 에러를 마주했고 리팩토링에 들어간다.
어떤 방식으로 만들지 고민하게 되는데...
import{Ref,forwardRef}from'react'typeInputProps={onChnage: ()=>void}constInput1=forwardRef(function(props: {/* A */},ref: {/* B */}){return<input{...props}ref={ref}type="text">})constInput2=forwardRef<{/* C */},{/* D */}>((props,ref)=>(<inputref={ref}{...props}type="text"/>))export{Input1,Input2}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
React에서
ref
관련 타입으로forwardRef
관련 문제를 내보려고 합니다.저는 항상 접할 때마다 헷갈리는 부분이네요.. 😇
여기서
T, P
의 위치가 사용할 때는 앞뒤가 바뀝니다.PropsWithoutRef<P>
는P
로 처음 위치하며ref
를 제외한props
타입을 받는다.RefAttributes<T>
는T
로 마지막에 위치하며ref
타입을 받는다.Input
사용 예시forwardRef
를 사용한 예시로 보면 2가지로 살펴볼 수 있습니다.문제 예시
종길이는 팀원들을 위해
Input
공통 컴포넌트를 만들었다.하지만
ref
를 생각하지 못해서 에러를 마주했고 리팩토링에 들어간다.어떤 방식으로 만들지 고민하게 되는데...
InputProps
, B:HTMLInputElement
, C:HTMLInputElement
, D:InputProps
InputProps
, B:HTMLInputElement
, C:InputProps
, D:HTMLInputElement
InputProps
, B:Ref<HTMLInputElement>
, C:HTMLInputElement
, D:InputProps
InputProps
, B:Ref<HTMLInputElement>
, C:InputProps
, D:HTMLInputElement
정답
3번1 vote ·
Beta Was this translation helpful? Give feedback.
All reactions