Skip to content

Commit

Permalink
docs: add more documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
wjaykim committed Dec 29, 2024
1 parent 9815a92 commit 260290d
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ You decide how and where they're placed, so the layout is more consistent your a

<img width="300" src="https://developers.google.com/static/admob/images/format-native.svg" alt="Native">

[Work In Progress](https://docs.page/invertase/react-native-google-mobile-ads/native-ads)
[Learn More](https://docs.page/invertase/react-native-google-mobile-ads/native-ads)

### Interstitial

Expand Down
69 changes: 69 additions & 0 deletions docs/native-ads.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,75 @@ const NativeComponent = () => {
};
```

## Receiving ad events

The `NativeAd` object emits events that you can listen to.

Use the `addAdEventListener` method to listen to ad events emitted by the `NativeAd`. Pass one of the `NativeAdEventType` enum values to specify the event type.

Check the [NativeAdEventType](https://github.com/invertase/react-native-google-mobile-ads/blob/main/src/NativeAdEventType.ts) source code to see the full range of events available.

```tsx
const NativeComponent = () => {
const [nativeAd, setNativeAd] = useState<NativeAd>();

useEffect(() => {
NativeAd.createForAdRequest(TestIds.NATIVE)
.then(setNativeAd)
.catch(console.error);
}, []);

useEffect(() => {
if (!nativeAd) return;
const listener = nativeAd.addAdEventListener(NativeAdEventType.CLICKED, () => {
console.log('Native ad clicked');
});
return () => {
listener.remove();
// or
nativeAd.destroy();
};
}, [nativeAd]);

return (
<NativeAdView nativeAd={nativeAd}>
// Components to display assets must be placed here
</NativeAdView>
);
};
```

## Destroying ads

Always destroy the `NativeAd` object when it is no longer needed to free up resources.

Calling the `destroy` method also removes all event listeners registered on the `NativeAd` object.

```tsx
const NativeComponent = () => {
const [nativeAd, setNativeAd] = useState<NativeAd>();

useEffect(() => {
NativeAd.createForAdRequest(TestIds.NATIVE)
.then(setNativeAd)
.catch(console.error);
}, []);

useEffect(() => {
if (!nativeAd) return;
return () => {
nativeAd.destroy();
};
}, [nativeAd]);

return (
<NativeAdView nativeAd={nativeAd}>
// Components to display assets must be placed here
</NativeAdView>
);
};
```

## Caveats / Limitations

### `NativeAsset` placement
Expand Down

0 comments on commit 260290d

Please sign in to comment.