infoWindow 렌더링 관련 질문 #112
Answered
by
zeakd
GyeongChan-Jang
asked this question in
Q&A
-
안녕하세요! https://zeakd.github.io/react-naver-maps/examples/map-tutorial-6-geolocation/ 이 예제에 useRef대신 useState로 infoWindow를 렌더링하는 이유가 있을까요? 혹시 state를 사용하신 다른 이유가 있으신지 궁금해서 여쭙습니다. |
Beta Was this translation helpful? Give feedback.
Answered by
zeakd
Oct 27, 2023
Replies: 1 comment 2 replies
-
일반적은 dom 은 ref 를 적용하였을 때 mount 단계에서 반드시 ref 가 존재하는데요. function Page() {
const ref = useRef(null);
useEffect(() => {
console.log(ref.current) // <div />
}, [])
console.log(ref.current) // null
return <div ref={ref} />
} dom 이 아닌 component 의 경우 ref 가 잡히는 시점이 mount 되는 시점이 아니기 때문에 ref 가 사용가능해지는 타이밍을 알기 어렵습니다. function Page() {
const ref = useRef();
useEffect(() => {
console.log(ref.current) // undefined (???)
}, [])
console.log(ref.current) // undefined
return <InfoWindow ref={ref} />
} 따라서 state 를 사용해 ref 가 업데이트 되는 시점을 가져옵니다. 타입은 아래처럼 작성해주시면 됩니다. function Page() {
const [ref, setRef] = useState<naver.maps.InfoWindow | undefined>();
useEffect(() => {
console.log(ref) // infowindow get
}, [ref])
console.log(ref) // undefined
return <InfoWindow ref={setRef} />
} |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
GyeongChan-Jang
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
일반적은 dom 은 ref 를 적용하였을 때 mount 단계에서 반드시 ref 가 존재하는데요.
dom 이 아닌 component 의 경우 ref 가 잡히는 시점이 mount 되는 시점이 아니기 때문에 ref 가 사용가능해지는 타이밍을 알기 어렵습니다.
따라서 state 를 사용해 ref 가 업데이트 되는 시점을 가져옵니다. 타입은 아래처럼 작성해주시면 됩니다.