react-nativemobileadmobexpobanner-ads

How to hide ad banner offline or when no ad could be loaded - in React Native with Expo?


I implemented ads with the expo and admob in my react native app, but I would like to get rid of the blank/blocking space when no ads are being loaded. Haven't found any examples for that. (other than the banner, I have a header and scrollview on that page). This is how the admob banner is implemented:

// Display a banner
<AdMobBanner
  bannerSize="fullBanner"
  adUnitID="ca-app-pub-3940256099942544/6300978111" // Test ID, Replace    with your-admob-unit-id
  testDeviceID="EMULATOR" />

Solution

  • My solution to this problem is to wrap the admob component in a view with styling to set height equal to zero. When you receive an ad. Then update the styling to show the banner in a view

    export class AdContainer extends React.Component {
      constructor() {
        super();
        this.state = { height: 0 };
      }
    
      bannerError(e) {
        console.log('banner error: ');
        console.log(e);
      }
      adReceived() {
        console.log('banner ad received: ');
        this.setState({
          ...this.state,
          hasAd: true
        });
      }
      adClicked() {
        console.log('banner ad clicked: ');
      }
    
      render() {
        return (
          <View style={!this.state.hasAd ? { height: 0 } : {}}>
            <View>
              <AdMobBanner
                bannerSize="smartBannerPortrait" // "largeBanner" "fullBanner"
                adUnitID={BANNER_ID}
                testDeviceID="EMULATOR"
                onDidFailToReceiveAdWithError={this.bannerError}
                onAdViewDidReceiveAd={this.adReceived.bind(this)}
                onAdViewWillPresentScreen={this.adClicked.bind(this)}
              />
            </View>
          </View>
        );
      }
    }