英文:
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 '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 = () => {
// ______________________ Request Configuration ___________________
useEffect(() => {
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(() => {
// Request config successfully set!
});
firebase.initializeApp();
});
// ____________ Interstetial Ads setup ________________________
const [loaded, setLoaded] = useState(false);
useEffect(() => {
const eventListener = interstitial.onAdEvent((type) => {
if (type === AdEventType.LOADED) {
setLoaded(true);
}
});
// Start loading the interstitial straight away
interstitial.load();
// Unsubscribe from events on unmount
return () => {
eventListener();
};
}, []);
// No advert ready to show yet
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;
build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
// apply plugin: 'com.google.gms.google-services' // Google Services plugin
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")
// classpath 'com.google.gms:google-services:4.3.4'
// 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("$rootDir/../node_modules/react-native/android")
}
maven {
// Android JSC is installed from npm
url("$rootDir/../node_modules/jsc-android/dist")
}
google()
jcenter()
maven { url 'https://www.jitpack.io' }
答案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(() => {
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(() => {
// Request config successfully set!
});
firebase.initializeApp();
}, []);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论