src / components / KakaoAdFit.tsx
// KakaoAdfit.jsx
import {useEffect, useRef} from "react";
// any 타입 대신 명시적 타입 정의
interface KakaoAdFitProps {
unit: string;
width: number;
height: number;
disabled?: boolean;
}
// window 타입 확장
declare global {
interface Window {
adfit?: {
destroy: (unit: string) => void;
};
}
}
function KakaoAdFit({unit, width, height, disabled}: KakaoAdFitProps) {
// HTMLDivElement 타입 명시
const scriptElementWrapper = useRef<HTMLDivElement>(null);
useEffect(() => {
if (!disabled) {
const script = document.createElement("script");
script.setAttribute("src", "https://t1.daumcdn.net/kas/static/ba.min.js");
// 옵셔널 체이닝으로 null 체크
scriptElementWrapper.current?.appendChild(script);
return () => {
const globalAdfit = window.adfit;
if (globalAdfit) globalAdfit.destroy(unit);
}
}
}, [unit, disabled])
return <div ref={scriptElementWrapper}>
<ins className="kakao_ad_area" style={{display: "none"}}
data-ad-unit={unit}
data-ad-width={width}
data-ad-height={height}></ins>
</div>
}
export default KakaoAdFit;
src / component / 카카오 애드핏 불러올 컴포넌트
return(
...
<KakaoAdFit unit={"DAN-5IEwe124OPFcM43H"} width={320} height={100} disabled={false} />
...
)
'프론트엔드 > React' 카테고리의 다른 글
[React] 리액트 전체화면 기능 예시 코드 (0) | 2025.02.12 |
---|---|
[React] 리액트에서 key의 역할( + 배열 데이터 변경으로 인한 렌더링 버그 트러블 슈팅) (0) | 2025.02.08 |
[React] 리액트의 useState와 Zustand의 store에 저장된 데이터는 브라우저의 어느 위치에 저장되는 것일까? (1) | 2024.12.18 |
[React] 배열 렌더링 key 속성 오류 원인 (0) | 2024.11.24 |
[React] useEffect와 useMemo의 차이점 (0) | 2024.09.17 |
src / components / KakaoAdFit.tsx
// KakaoAdfit.jsx
import {useEffect, useRef} from "react";
// any 타입 대신 명시적 타입 정의
interface KakaoAdFitProps {
unit: string;
width: number;
height: number;
disabled?: boolean;
}
// window 타입 확장
declare global {
interface Window {
adfit?: {
destroy: (unit: string) => void;
};
}
}
function KakaoAdFit({unit, width, height, disabled}: KakaoAdFitProps) {
// HTMLDivElement 타입 명시
const scriptElementWrapper = useRef<HTMLDivElement>(null);
useEffect(() => {
if (!disabled) {
const script = document.createElement("script");
script.setAttribute("src", "https://t1.daumcdn.net/kas/static/ba.min.js");
// 옵셔널 체이닝으로 null 체크
scriptElementWrapper.current?.appendChild(script);
return () => {
const globalAdfit = window.adfit;
if (globalAdfit) globalAdfit.destroy(unit);
}
}
}, [unit, disabled])
return <div ref={scriptElementWrapper}>
<ins className="kakao_ad_area" style={{display: "none"}}
data-ad-unit={unit}
data-ad-width={width}
data-ad-height={height}></ins>
</div>
}
export default KakaoAdFit;
src / component / 카카오 애드핏 불러올 컴포넌트
return(
...
<KakaoAdFit unit={"DAN-5IEwe124OPFcM43H"} width={320} height={100} disabled={false} />
...
)
'프론트엔드 > React' 카테고리의 다른 글
[React] 리액트 전체화면 기능 예시 코드 (0) | 2025.02.12 |
---|---|
[React] 리액트에서 key의 역할( + 배열 데이터 변경으로 인한 렌더링 버그 트러블 슈팅) (0) | 2025.02.08 |
[React] 리액트의 useState와 Zustand의 store에 저장된 데이터는 브라우저의 어느 위치에 저장되는 것일까? (1) | 2024.12.18 |
[React] 배열 렌더링 key 속성 오류 원인 (0) | 2024.11.24 |
[React] useEffect와 useMemo의 차이점 (0) | 2024.09.17 |