> For the complete documentation index, see [llms.txt](https://partner-docs.covergenius.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://partner-docs.covergenius.com/xcover-journeys/integration/messaging/react-native.md).

# React Native

Send and receive journey events through a WebView bridge in React Native.

{% hint style="info" %}
There is no separate native SDK or bridge shipped by Cover Genius for React Native. You relay the same JSON envelope described on the main Messaging page through `react-native-webview`'s standard `onMessage`/`injectedJavaScript` mechanism.

This page shows the CG specific envelope and events; for the underlying bridge mechanism itself, see the official [react-native-webview guide: Communicating between JS and Native](https://github.com/react-native-webview/react-native-webview/blob/master/docs/Guide.md#communicating-between-js-and-native).
{% endhint %}

### Prerequisites

`react-native-webview` (or an equivalent WebView wrapper) installed and rendering the journey URL.

### Receiving events from the journey

Inject a script that forwards `window.postMessage` calls to React Native's `onMessage`, and handle them in `onMessage`:

```tsx
import React, { useRef } from 'react';
import WebView from 'react-native-webview';

const BRIDGE_SCRIPT = `
  window.addEventListener('message', function(event) {
    if (event.data && event.data.source === 'xcover-journeys') {
      window.ReactNativeWebView.postMessage(JSON.stringify(event.data));
    }
  });
  true;
`;

export function JourneyScreen() {
  const webViewRef = useRef<WebView>(null);

  const handleMessage = (event: { nativeEvent: { data: string } }) => {
    const { type, payload } = JSON.parse(event.nativeEvent.data);

    switch (type) {
      case 'CG_JOURNEY_READY':
        console.log('Journey mounted and ready');
        break;
      case 'CG_JOURNEY_COMPLETE':
        console.log('Journey finished:', payload.outcome, payload.policy_id);
        break;
    }
  };

  return (
    <WebView
      ref={webViewRef}
      source={{ uri: 'https://xcj.xcover.com/acme-travel/en/?country=AU' }}
      injectedJavaScript={BRIDGE_SCRIPT}
      onMessage={handleMessage}
    />
  );
}
```

### Sending events into the journey

Use `injectJavaScript` on the `WebView` ref to dispatch a `message` event the journey's listener picks up:

```tsx
function sendToJourney(webViewRef: React.RefObject<WebView>, type: string, payload: object = {}) {
  const js = `
    window.postMessage({ source: 'xcover-journeys', type: '${type}', payload: ${JSON.stringify(payload)} }, '*');
    true;
  `;
  webViewRef.current?.injectJavaScript(js);
}

// Example: ask the journey to hide itself
sendToJourney(webViewRef, 'CG_JOURNEY_HIDE');
```

### Origin and security notes

The journey's own trusted-origin validation is designed for browser `postMessage` traffic (checked against `event.origin`).

Inside a React Native `WebView` bridge, the risk model shifts instead to this: only your injected bridge script can call `ReactNativeWebView.postMessage`, and only code running inside the loaded page (the journey itself) can trigger it.

Load only the journey URL provided for your integration, and avoid attaching this bridge to a `WebView` that can navigate to arbitrary or user-supplied URLs.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://partner-docs.covergenius.com/xcover-journeys/integration/messaging/react-native.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
