Skip to content

infoWindow 렌더링 관련 질문 #112

Answered by zeakd
GyeongChan-Jang asked this question in Q&A
Discussion options

You must be logged in to vote

일반적은 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>();
  
  useE…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@zeakd
Comment options

@GyeongChan-Jang
Comment options

Answer selected by GyeongChan-Jang
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants