This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.
Expo Brownfield
用于将 Expo 集成到现有原生应用中的工具包和 API。
expo-brownfield 是一个用于向现有原生 Android 和 iOS 应用中添加 React Native 视图的工具包。它提供:
- 内置 API,用于原生应用和 React Native 应用之间的双向通信与导航
- 配置插件,用于在你的 Expo 项目中自动设置 brownfield 目标
- CLI,用于构建并将产物发布到 Maven 仓库(Android)和 XCFrameworks(iOS)
安装
If you are installing this in an existing React Native app, make sure to install expo in your project.
用法
通信 API
通信 API 支持原生(宿主)应用与 React Native 之间基于消息的双向通信。
从 React Native 向原生发送消息
import * as Brownfield from 'expo-brownfield'; Brownfield.sendMessage({ type: 'MyMessage', data: { language: 'TypeScript', expo: true, platforms: ['android', 'ios'], }, });
在 React Native 中接收来自原生的消息
import * as Brownfield, { type MessageEvent } from 'expo-brownfield'; import { useEffect } from 'react'; function MyComponent() { useEffect(() => { const handleMessage = (event: MessageEvent) => { console.log('收到消息:', event); }; Brownfield.addMessageListener(handleMessage); return () => { Brownfield.removeMessageListener(handleMessage); }; }, []); // ... }
从原生向 React Native 发送消息
import expo.modules.brownfield.BrownfieldMessaging BrownfieldMessaging.sendMessage(mapOf( "type" to "MyAndroidMessage", "timestamp" to System.currentTimeMillis(), "data" to mapOf( "platform" to "android" ) ))
import ExpoBrownfield BrownfieldMessaging.sendMessage([ "type": "MyIOSMessage", "timestamp": Date().timeIntervalSince1970, "data": [ "platform": "ios" ] ])
在原生中接收来自 React Native 的消息
import expo.modules.brownfield.BrownfieldMessaging val listenerId = BrownfieldMessaging.addListener { event -> println("来自 React Native 的消息:$event") } // Later, to remove the listener: BrownfieldMessaging.removeListener(listenerId)
import ExpoBrownfield let listenerId = BrownfieldMessaging.addListener { message in print("来自 React Native 的消息:\(message)") } // Later, to remove the listener: BrownfieldMessaging.removeListener(id: listenerId)
app 配置中的配置
expo-brownfield 包提供了一个配置插件,可在使用连续原生生成(CNG)时用于配置 brownfield 集成。此插件允许你自定义 Expo 项目的打包方式,以及它如何集成到现有的原生应用中。
Example app.json with config plugin
Configurable properties
CLI
expo-brownfield 库包含一个用于构建并发布到 Maven 仓库(Android)和 XCFrameworks(iOS)的 CLI。
命令
build:android
构建并发布 brownfield 库及其依赖到 Maven 仓库。
build:ios
构建 brownfield XCFramework,并将 Hermes XCFramework 复制到 artifacts 目录。
tasks:android
列出所有可用的发布任务和 Maven 仓库。
API
import * as Brownfield from 'expo-brownfield';
Hooks
Hook to observe and set the value of shared state for a given key.
Provides a synchronous API similar to useState.
[T | undefined, (value: T | (prev: T | undefined) => T) => void]A tuple containing the value and a function to set the value.
Methods
Gets the number of registered message listeners.
numberThe number of active message listeners.
Gets the value of shared state for a given key.
T | undefinedNavigates back to the native part of the app, dismissing the React Native view.
voidSends a message to the native side of the app. The message can be received by setting up a listener in the native code.
voidEnables or disables the native back button behavior. When enabled, pressing the back button will navigate back to the native part of the app instead of performing the default React Navigation back action.
voidSets the value of shared state for a given key.
voidEvent subscriptions
Adds a listener for messages sent from the native side of the app.
EventSubscriptionA subscription object that can be used to remove the listener.
Example
const subscription = addMessageListener((event) => { console.log('Received message from native:', event); }); // Later, to remove the listener: subscription.remove();
Adds a listener for changes to the shared state for a given key.
EventSubscriptionA subscription object that can be used to remove the listener.
Removes a specific message listener.
voidInterfaces
A subscription object that allows to conveniently remove an event listener from the emitter.