Banner ads
<BannerAd> is a NitroUI view. It loads an ad once adUnitId is set and lays itself out at the ad size.
import { Admob, BannerAd } from 'nitro-ui-admob'
function Footer() {
return (
<BannerAd
adUnitId={Admob.testAdUnitId('banner')}
layout={{ marginTop: 24 }}
onAdLoaded={({ width, height }) => console.log(width, height)}
onAdFailedToLoad={(error) => console.warn(error.code, error.message)}
/>
)
}
Sizes
size | Width × height (pt on iOS, dp on Android) |
|---|---|
anchoredAdaptive (default) | Laid-out width × height chosen by the SDK |
banner | 320 × 50 |
largeBanner | 320 × 100 |
mediumRectangle | 300 × 250 |
fullBanner | 468 × 60 |
leaderboard | 728 × 90 |
Fixed sizes
A fixed-size banner lays out at its ad size, so you do not need to reserve space:
<BannerAd adUnitId={adUnitId} size="mediumRectangle" />
Anchored adaptive
anchoredAdaptive uses the width the view is laid out with, and asks the SDK for a large anchored adaptive banner of that width. The view starts at height 0, then takes the height reported through onSizeChange before the request. Its width comes from its container or from layout.
Nothing loads until the view has a width greater than 0. A new width loads a new ad.
Overriding the size
layout props override the size-derived width and height. The banner stays centered in the view at its ad size.
When a new ad loads
The view requests a new ad when any of these change:
adUnitId(an empty string loads nothing),size,- the contents of
requestOptions, - for
anchoredAdaptive, the laid-out width.
Passing a new requestOptions object with the same contents does not reload.
Events
| Prop | Called when |
|---|---|
onSizeChange | Before each request, with the ad size, so the layout can reserve it. |
onAdLoaded | An ad loaded, with its size. |
onAdFailedToLoad | An ad request failed, with an AdmobError. |
onAdImpression | The SDK recorded an impression. |
onAdClicked | The SDK recorded a click. |
onAdPaid | The SDK estimated the impression's revenue. |
onAdOpened | The ad opened an overlay over the app. |
onAdClosed | The overlay opened by the ad was dismissed. |
Unmounting
On unmount the component calls the native view's destroy(): ad requests stop, callbacks are dropped and the native banner is released.