import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { View, Button, Dimensions } from 'react-native'
import { WebView } from 'react-native-webview'
import {
AdropNativeAd,
AdropNativeAdListener,
AdropNativeAdView,
AdropProfileLogoView,
AdropProfileNameView,
AdropHeadLineView,
AdropBodyView,
} from '@adrop/react-native-ads'
const YourComponent: React.FC = () => {
const [nativeAd, setNativeAd] = useState<AdropNativeAd>()
const [isLoaded, setIsLoaded] = useState(false)
const initialize = useCallback(
(unitId: string) => {
// 비디오 광고 컨트롤 활성화를 위해 useCustomClick을 true로 설정
let adropNativeAd = new AdropNativeAd(unitId, true)
adropNativeAd.listener = listener
setNativeAd((prev) => {
prev?.destroy()
return adropNativeAd
})
},
[listener]
)
const listener = useMemo(
(): AdropNativeAdListener => ({
onAdReceived: (ad: AdropNativeAd) => {
console.log(`nativeAd received ${ad.unitId}`, ad.properties)
// 커스텀 필드를 추가하셨다면, ad.properties.extra 에서 추가된 값들을 받으실 수 있습니다.
setIsLoaded(true)
},
onAdFailedToReceive: (_, error) => {
console.log('nativeAd onAdFailedToReceive', error)
},
onAdClicked: (ad) => console.log(`nativeAd clicked ${ad.unitId}`),
}),
[]
)
useEffect(() => {
initialize(testUnitId_native)
}, [initialize])
const load = () => nativeAd?.load()
// WebView로 비디오/이미지 광고 렌더링
const renderMediaContent = () => {
if (!nativeAd?.properties?.creative) return null
return (
<WebView
source={{ html: nativeAd.properties.creative }}
style={styles.adStyle}
javaScriptEnabled={true}
domStorageEnabled={true}
allowsInlineMediaPlayback={true}
mediaPlaybackRequiresUserAction={false}
// 비디오 광고를 위한 설정
allowsFullscreenVideo={true}
bounces={false}
scrollEnabled={false}
onShouldStartLoadWithRequest={() => true}
/>
)
}
const adView = useMemo(() => {
if (!isLoaded) return null
return (
<AdropNativeAdView
nativeAd={nativeAd}
style={{
...styles.adContainer,
width: Dimensions.get('window').width,
}}
>
<View style={styles.rowContainer}>
<AdropProfileLogoView style={styles.icon} />
<AdropProfileNameView style={styles.name} />
</View>
<AdropHeadLineView style={styles.headline} />
<AdropBodyView style={styles.body} />
{/* AdropMediaView 대신 WebView 사용 */}
{renderMediaContent()}
</AdropNativeAdView>
)
}, [isLoaded, nativeAd])
return (
<View>
<View>
<Button title={'nativeAd load'} onPress={load} />
</View>
<View>{adView}</View>
</View>
)
}