On App.js I have initialized AdMobRewarded as following:
if (Platform.OS === 'ios') { AdMobRewarded.setAdUnitID('ca-app-pub-xxx/xxx');} else { AdMobRewarded.setAdUnitID('ca-app-pub-xxx/xxx');}
And here is the class:
export default class App extends React.Component { state = { fontsLoaded: false, }; render() { const { fontsLoaded } = this.state; if (!fontsLoaded) { return (<AppLoading startAsync={fetchFonts} onFinish={() => this.setState({ fontsLoaded: true })} /> ); } return (<Provider store={store}><AppContainer ref={navigatorRef => { NavigationService.setTopLevelNavigator(navigatorRef); }} /><CommonComponents /></Provider> ); }}
Inside the CommonComponents I have put the listener for AdMobRewarded:
useEffect(() => { AdMobRewarded.addEventListener('rewardedVideoDidRewardUser', () => { setState({ hintModalVisible: true, adIsLoading: false, mainMenuVisible: false, }); }); return () => { AdMobRewarded.removeAllListeners(); };}, []);
setState is actually not React setState, it's a redux action I have implemented:
const setStateAction = (obj, sceneName) => { const type = sceneName ? `${sceneName}_SET_STATE` : 'SET_STATE'; return { ...obj, type };};
Without the rewardedVideoDidRewardUser
listener, calling setState
does open the Modal and everything is fine.
hintModalVisible
is used for Modal isVisible
prop, which opens and closes the Modal.
On Android everything works as expected, but there is a strange behavior on iOS. The ad shows for a second and automatically closes, and the Hint Modal doesn't open.
It's an open source project, so you can the code here