当应用程序运行时,唯一显示的是一个空白白屏:React Native。

huangapple go评论80阅读模式
英文:

when app is running the only thing that is displayed is a blank white screen :- React Native

问题

import React, { useEffect, useState } from 'react';
import { Button, View } from 'react-native';
import admob, { MaxAdContentRating } from '@react-native-firebase/admob';
import {
  BannerAd,
  BannerAdSize,
  InterstitialAd,
  AdEventType,
} from '@react-native-firebase/admob';
import firebase from 'firebase';

const BanneradUnitId = 'ca-app-pub-7184661797309439/5996869501';

const InterstetialadUnitId = 'ca-app-pub-7184661797309439/5399949611';

const interstitial = InterstitialAd.createForAdRequest(InterstetialadUnitId, {
  requestNonPersonalizedAdsOnly: true,
  keywords: ['fashion', 'clothing'],
});

const App = () => {
  useEffect(() => {
    admob()
      .setRequestConfiguration({
        maxAdContentRating: MaxAdContentRating.PG,
        tagForChildDirectedTreatment: true,
        tagForUnderAgeOfConsent: true,
      })
      .then(() => {
        // Request config successfully set!
      });
    firebase.initializeApp();
  });

  const [loaded, setLoaded] = useState(false);

  useEffect(() => {
    const eventListener = interstitial.onAdEvent((type) => {
      if (type === AdEventType.LOADED) {
        setLoaded(true);
      }
    });

    interstitial.load();

    return () => {
      eventListener();
    };
  }, []);

  if (!loaded) {
    return null;
  }

  return (
    <View className={styles.main}>
      <h1>Hello World</h1>
      <BannerAd
        unitId={BanneradUnitId}
        size={BannerAdSize.FULL_BANNER}
        requestOptions={{
          requestNonPersonalizedAdsOnly: true,
        }}
      />
      <BannerAd
        unitId={BanneradUnitId}
        size={BannerAdSize.FULL_BANNER}
        requestOptions={{
          requestNonPersonalizedAdsOnly: true,
        }}
      />
      <BannerAd
        unitId={BanneradUnitId}
        size={BannerAdSize.FULL_BANNER}
        requestOptions={{
          requestNonPersonalizedAdsOnly: true,
        }}
      />
      <BannerAd
        unitId={BanneradUnitId}
        size={BannerAdSize.FULL_BANNER}
        requestOptions={{
          requestNonPersonalizedAdsOnly: true,
        }}
      />
      <Button
        title="Show Interstitial"
        onPress={() => {
          interstitial.show();
        }}
      />
    </View>
  );
};

const styles = StyleSheet.create({
  main: {
    textAlign: 'center',
  },
});

export default App;
buildscript {
    ext {
        buildToolsVersion = "29.0.2"
        minSdkVersion = 16
        compileSdkVersion = 29
        targetSdkVersion = 29
    }
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath("com.android.tools.build:gradle:3.5.3")
    }
}

allprojects {
    repositories {
        mavenLocal()
        maven {
            url("$rootDir/../node_modules/react-native/android")
        }
        maven {
            url("$rootDir/../node_modules/jsc-android/dist")
        }

        google()
        jcenter()
        maven { url 'https://www.jitpack.io' }
    }
}
英文:

I have created an app on React Native , I have been stuck in one place from past week . When the app is initialized the only thing that is seen is blank white screen all the time I have changed ""div"" to ""view"" as someone suggested but still it gives the same output . Please provide a solution to it.
Best Regards

APP.JS

import React, {useEffect, useState} from &#39;react&#39;;
import {Button,View} from &#39;react-native&#39;;
import admob, {MaxAdContentRating} from &#39;@react-native-firebase/admob&#39;;
import {
BannerAd,
BannerAdSize,
InterstitialAd,
AdEventType,
} from &#39;@react-native-firebase/admob&#39;;
import firebase from &#39;firebase&#39;;
const BanneradUnitId = &#39;ca-app-pub-7184661797309439/5996869501&#39;;
const InterstetialadUnitId = &#39; ca-app-pub-7184661797309439/5399949611&#39;;
const interstitial = InterstitialAd.createForAdRequest(InterstetialadUnitId, {
requestNonPersonalizedAdsOnly: true,
keywords: [&#39;fashion&#39;, &#39;clothing&#39;],
});
const App = () =&gt; {
// ______________________ Request Configuration ___________________
useEffect(() =&gt; {
admob()
.setRequestConfiguration({
// Update all future requests suitable for parental guidance
maxAdContentRating: MaxAdContentRating.PG,
// Indicates that you want your content treated as child-directed for purposes of COPPA.
tagForChildDirectedTreatment: true,
// Indicates that you want the ad request to be handled in a
// manner suitable for users under the age of consent.
tagForUnderAgeOfConsent: true,
})
.then(() =&gt; {
// Request config successfully set!
});
firebase.initializeApp();
});
// ____________ Interstetial Ads setup ________________________
const [loaded, setLoaded] = useState(false);
useEffect(() =&gt; {
const eventListener = interstitial.onAdEvent((type) =&gt; {
if (type === AdEventType.LOADED) {
setLoaded(true);
}
});
// Start loading the interstitial straight away
interstitial.load();
// Unsubscribe from events on unmount
return () =&gt; {
eventListener();
};
}, []);
// No advert ready to show yet
if (!loaded) {
return null;
}
return (
&lt;View className={styles.main}&gt;
&lt;h1&gt;Hello World&lt;/h1&gt;
&lt;BannerAd
unitId={BanneradUnitId}
size={BannerAdSize.FULL_BANNER}
requestOptions={{
requestNonPersonalizedAdsOnly: true,
}}
/&gt;
&lt;BannerAd
unitId={BanneradUnitId}
size={BannerAdSize.FULL_BANNER}
requestOptions={{
requestNonPersonalizedAdsOnly: true,
}}
/&gt;
&lt;BannerAd
unitId={BanneradUnitId}
size={BannerAdSize.FULL_BANNER}
requestOptions={{
requestNonPersonalizedAdsOnly: true,
}}
/&gt;
&lt;BannerAd
unitId={BanneradUnitId}
size={BannerAdSize.FULL_BANNER}
requestOptions={{
requestNonPersonalizedAdsOnly: true,
}}
/&gt;
&lt;Button
title=&quot;Show Interstitial&quot;
onPress={() =&gt; {
interstitial.show();
}}
/&gt;
&lt;/View&gt;
);
};
const styles = StyleSheet.create({
main: {
textAlign: &#39;center&#39;,
},
});
export default App;

build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.
// apply plugin: &#39;com.google.gms.google-services&#39;  // Google Services plugin
buildscript {
ext {
buildToolsVersion = &quot;29.0.2&quot;
minSdkVersion = 16
compileSdkVersion = 29
targetSdkVersion = 29
}
repositories {
google()
jcenter()
}
dependencies {
classpath(&quot;com.android.tools.build:gradle:3.5.3&quot;)
// classpath &#39;com.google.gms:google-services:4.3.4&#39;
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
mavenLocal()
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url(&quot;$rootDir/../node_modules/react-native/android&quot;)
}
maven {
// Android JSC is installed from npm
url(&quot;$rootDir/../node_modules/jsc-android/dist&quot;)
}
google()
jcenter()
maven { url &#39;https://www.jitpack.io&#39; }

答案1

得分: 1

// 以下是翻译后的内容:

 `useEffect` 中初始化 Firebase 配置时确保添加依赖项或者仅使用空数组进行初始化类似于 `onDidMount`)。

尝试将代码更改为

<!-- 开始代码片段: js 隐藏: false 控制台输出: true Babel转换: false -->

<!-- 语言: lang-js -->

useEffect(() => {
    admob()
      .setRequestConfiguration({
        // 更新所有将来适用于家长指导的请求
        maxAdContentRating: MaxAdContentRating.PG,

        // 表示您希望将您的内容视为面向儿童,以符合 COPPA 要求。
        tagForChildDirectedTreatment: true,

        // 表示您希望广告请求的处理方式适合未满法定年龄的用户。
        tagForUnderAgeOfConsent: true,
      })
      .then(() => {
        // 请求配置成功设置!
      });
    firebase.initializeApp();
}, []);

<!-- 结束代码片段 -->
英文:

You firebase initialize config on useEffect doesn't have dependencies. Make sure to put the dependencies or just empty array for initialize purpose (onDidMount).

try change to this:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

useEffect(() =&gt; {
admob()
.setRequestConfiguration({
// Update all future requests suitable for parental guidance
maxAdContentRating: MaxAdContentRating.PG,
// Indicates that you want your content treated as child-directed for purposes of COPPA.
tagForChildDirectedTreatment: true,
// Indicates that you want the ad request to be handled in a
// manner suitable for users under the age of consent.
tagForUnderAgeOfConsent: true,
})
.then(() =&gt; {
// Request config successfully set!
});
firebase.initializeApp();
}, []);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年10月20日 00:34:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64431598.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定