일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- classList add
- 냥빨이다가온다
- react
- 리엑트할수있는퍼블리셔라규
- chart responsive
- classList remove
- 를
- chart.js
- 배열중복제거
- classList
- 내배위의고양이
- 포트폴리오
- 대표님물리기없기
- ROUTER 버전6
- 간식금지다!
- 방심은금물
- set()
- new set()
- 2020
- 커스텀훅
- 퍼블리싱
- 안짤렸다
- react-router-dom
- 언젠간가겠지
- classList toggle
- react-router
- 클래스 토글
- className
- windowwidth
- 접근성
Archives
- Today
- Total
틈
[REACT] 외부 API 받아오기(with typescript) -declare 본문
카카오맵 API를 react에서 작업할 때
typescript를 template으로 사용하고 있다면,
index.html에 API를 추가해도 kakao 객체의 존재 여부를 typescript에서 인식하지 못한다.
-- 아래는 해당 에러이다.
이 경우에 typescript에게 kakao 라는 객체가 window에 존재하고 있다고 인식시켜줘야 하는데,
그럴 때 사용하는 typescript 문법이 declare이다.
import React, { useEffect } from "react";
import "./App.css";
declare global {
interface Window {
kakao: any;
}
}
function App() {
useEffect(() => {
var container = document.getElementById("map"); //지도를 담을 영역의 DOM 레퍼런스
var options = {
//지도를 생성할 때 필요한 기본 옵션
center: new window.kakao.maps.LatLng(33.450701, 126.570667), //지도의 중심좌표.
level: 3, //지도의 레벨(확대, 축소 정도)
};
var map = new window.kakao.maps.Map(container, options); //지도 생성 및 객체 리턴
return () => {};
}, []);
return (
<div className="App">
<div id="map" style={{ width: 300, height: 300 }}></div>
</div>
);
}
export default App;
또한, kakao 객체가 window 하위 객체라는 것을 정의해야 하므로 window.kakao[~~] 로 변경한다.
declare 문법은 계속 공부하면서 효용을 더 찾아보는걸로...
'code > React.js' 카테고리의 다른 글
[React] React에서 반응형 웹 대응하기 (0) | 2025.02.04 |
---|---|
[REACT] kakao지도 (0) | 2022.06.02 |
[REACT] useImperativeHandle, forwardRef hook (0) | 2022.05.17 |
[REACT] react 설치 방법 변경 (0) | 2022.05.13 |
[REACT] 유용한 dependencies (0) | 2022.05.07 |
Comments