> ## Documentation Index
> Fetch the complete documentation index at: https://docs.trygravity.com/llms.txt
> Use this file to discover all available pages before exploring further.

# React Native SDK

> Show Gravity ads as native views in React Native apps.

Use `@gravity-ai/react-native` to show Gravity ads as native views. Your server requests the ads with the [JavaScript SDK](https://docs.trygravity.com/sdks/javascript.md), and your app shows them with `GravityAd`. For the terms on this page, see [Core concepts](https://docs.trygravity.com/introduction/concepts.md).

## 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](https://docs.trygravity.com/sdks/javascript.md).

The package does not use the DOM or `react-dom`.

## Install

```bash theme={null}
npm install @gravity-ai/react-native
```

## 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.

```ts theme={null}
import { GRAVITY_RN_SPEC_OPTIONS } from '@gravity-ai/react-native';

const result = await gravity.getAds(req, messages, placements, {
  ...GRAVITY_RN_SPEC_OPTIONS,
  enableFrames: false,
});
```

## Show ads

Give the ad from Gravity to `GravityAd`:

```tsx theme={null}
import { Text } from 'react-native';
import { GravityAd, GravityThemeProvider } from '@gravity-ai/react-native';

export function ChatResponse({ response, ads }) {
  return (
    <GravityThemeProvider>
      <Text>{response}</Text>
      {ads[0] && <GravityAd ad={ads[0]} />}
    </GravityThemeProvider>
  );
}
```

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](https://docs.trygravity.com/engine/contextual-ads.md#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:

```tsx theme={null}
<GravityAd ad={ad} isViewable={visiblePlacementIds.has(String(ad.placement_id))} />
```

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:

```tsx theme={null}
<GravityThemeProvider theme={{ primary: '#6d28d9' }}>
```

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

| Problem | Cause and fix |
| - | - |
| The ad shows as a plain text card, not the placement design | The request did not include `GRAVITY_RN_SPEC_OPTIONS`. Add it to the `getAds()` options on the server. |
| No image on image ads | Your version is older than 0.2.0. Upgrade `@gravity-ai/react-native`. |
| Impressions do not count in a `FlatList` | Pass `isViewable` from `onViewableItemsChanged`. |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.