@gravity-ai/react-native to show Gravity ads as native views. Your server requests the ads with the JavaScript SDK, and your app shows them with GravityAd. For the terms on this page, see Core concepts.
Before you start
- Your app uses
react18.2 or later andreact-native0.76 or later. These are peer dependencies. - Your server requests ads with
@gravity-ai/api. See JavaScript and React SDKs.
react-dom.
Install
Request ads
On your server, addGRAVITY_RN_SPEC_OPTIONS to the getAds() options. These options tell Gravity to send designs that the native renderer can draw (supportsSpec: true and specCapabilities: "psp1"). Also set enableFrames: false. The React Native SDK can’t show Gravity frames (rich frame and carousel ads), and @gravity-ai/api asks for them by default.
Show ads
Give the ad from Gravity toGravityAd:
GravityAd draws the placement design, shows a built-in card when no design is available, records the impression and the click, shows the ad label, and supports light and dark themes. From version 0.2.0, it also draws image ads. The impression counts one time, when at least 50% of the ad is visible.
Visibility in lists
In a plainScrollView, you do not need to do anything. The SDK measures visibility for you.
In a virtualized list (FlatList or SectionList), use onViewableItemsChanged to find the visible rows. Then set isViewable to true when the list reports the ad row as visible:
onScroll handler, use the measureVisibility function from the useAdTracking hook.
Themes
GravityThemeProvider follows the color scheme of the device by default. To change the colors, give theme tokens:
background, foreground, card, cardForeground, primary, primaryForeground, muted, mutedForeground, accent, accentForeground, border, and radius. To set light or dark mode, use the mode prop ("light", "dark", or "auto").