Skip to main content
Use @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 react 18.2 or later and react-native 0.76 or later. These are peer dependencies.
  • Your server requests ads with @gravity-ai/api. See JavaScript and React SDKs.
The package does not use the DOM or react-dom.

Install

Request ads

On your server, add GRAVITY_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 to GravityAd:
You see the ad under the response. 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 plain ScrollView, 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:
The impression then counts when the list reports the ad as visible. If you measure visibility in your own 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:
The tokens are 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").

Troubleshoot