JavaScript Module

Besides native components, we are exposing JavaScript functions to control the behavior of those components from React Native app.

Usage

To use the module, import it:

import ReactNativeBrownfield from '@callstack/react-native-brownfield';

Methods

setNativeBackGestureAndButtonEnabled

A method used to toggle iOS native back gesture and Android hardware back button.

ReactNativeBrownfield.setNativeBackGestureAndButtonEnabled(enabled: boolean);

Parameters:

ParamTypeDescription
enabledbooleanWhether to enable native back gesture and button

Example:

ReactNativeBrownfield.setNativeBackGestureAndButtonEnabled(true);

popToNative

A method to pop to native screen used to push React Native experience.

ReactNativeBrownfield.popToNative(animated?: boolean);

Parameters:

ParamTypeDescription
animatedbooleanOptional. Whether to animate the transition (iOS only). Defaults to false, has no effect on Android.

Example:

ReactNativeBrownfield.popToNative(true);

postMessage

Send a JSON-serializable message to the native host application. This resembles the web window.postMessage API.

ReactNativeBrownfield.postMessage(data: any);

Parameters:

ParamTypeDescription
dataanyAny JSON-serializable value to send to the native host
NOTE

This method is available both on the New Architecture and the Old Architecture.

Example:

ReactNativeBrownfield.postMessage({ action: 'greet', name: 'Alice' });

onMessage

Subscribe to messages sent from the native host application. Returns a subscription object with a remove() method for cleanup.

const subscription = ReactNativeBrownfield.onMessage(
  callback: (event: MessageEvent) => void
);

Parameters:

ParamTypeDescription
callback(event: MessageEvent) => voidHandler invoked for each incoming message

The MessageEvent object has a data property containing the deserialized message payload.

Returns: { remove: () => void } - call remove() to unsubscribe.

Example:

import ReactNativeBrownfield from '@callstack/react-native-brownfield';
import type { MessageEvent } from '@callstack/react-native-brownfield';

const subscription = ReactNativeBrownfield.onMessage((event: MessageEvent) => {
  console.log('Received from native:', event.data);
});

// Later, to unsubscribe:
subscription.remove();

Full example with React hooks:

import { useEffect, useState } from 'react';
import ReactNativeBrownfield from '@callstack/react-native-brownfield';
import type { MessageEvent } from '@callstack/react-native-brownfield';

function MyComponent() {
  const [lastMessage, setLastMessage] = useState<unknown>(null);

  useEffect(() => {
    const sub = ReactNativeBrownfield.onMessage((event: MessageEvent) => {
      setLastMessage(event.data);
    });
    return () => sub.remove();
  }, []);

  return (
    <Button
      title="Send to Native"
      onPress={() => {
        ReactNativeBrownfield.postMessage({ greeting: 'Hello!' });
      }}
    />
  );
}
Note

These methods work only with native components provided by this library.

Need React or React Native expertise you can count on?