React Native Brownfield provides first-class support for Kotlin.
With react-native >= 0.80.0, an auto-generated file was added which is responsible to load your App's native libs. If you're consuming this library in a RN project, then you will have this file ReactNativeApplicationEntryPoint available. If you're consuming this library in a RN android library which is backed by com.callstack.react:brownfield-gradle-plugin, then this file will also be available.
Below is the code you need to add before you call ReactNativeBrownfield.initialize:
Previously, you were required to implement DefaultHardwareBackBtnHandler in your calling Activity. Now with > 1.1.0 you are not required to do that step. If you're upgrading to the latest version then you can safely remove that interface implementation from your calling Activity.
ReactNativeBrownfieldYou can import the object from:
initializeA function used to initialize a React Native Brownfield singleton. Keep in mind that it doesn't load React Native bundle.
| Param | Required | Type | Description |
|---|---|---|---|
| application | Yes | Application | Main application. |
| reactHost | Exclusively* | ReactHost | An instance of ReactHost. |
| packages | Exclusively* | List<ReactPackage> | List of your React Native Native modules. |
| options | Exclusively* | HashMap<String, Any> | Map of initial options. Options listed below. |
| onJSBundleLoaded | Exclusively* | OnJSBundleLoaded | Callback invoked after JS bundle is fully loaded. |
*- From the marked fields, exactly one must be specified, excluding the others. See examples below.
Available options:
useDeveloperSupport: Boolean - Flag to use dev support.packages: List<ReactPackage> - List of your React Native Native modules.mainModuleName: String - Path to react native entry file (when loading from Metro).bundleAssetPath: String - Path to react native bundle asset file (when loading from app assets), appended to asset://.Examples:
sharedA singleton that keeps an instance of ReactNativeBrownfield object.
| Property | Type | Default | Description |
|---|---|---|---|
| reactHost | ReactHost | null | An instance of ReactHost. |
createViewCreates a React Native view with a given module name. It automatically uses an instance of React Native created in initialize method. This is useful when embedding React Native views directly in your native layouts or Jetpack Compose UI.
| Param | Required | Type | Description |
|---|---|---|---|
| context | Yes | Context | Android context to create the view |
| activity | No | FragmentActivity | Activity hosting the view, used for lifecycle management |
| moduleName | Yes | String | Name of React Native component registered to AppRegistry |
| launchOptions | No | Bundle | Initial properties to be passed to React Native component |
Returns: FrameLayout - A view containing the React Native component.
Examples:
ReactNativeFragmentA fragment rendering ReactRootView with a given module name. It automatically uses an instance of React Native created in initialize method. It works well with exposed JavaScript module. All the lifecycles are proxied to ReactInstanceManager. It's the simplest way to embed React Native into your navigation stack.
createReactNativeFragmentCreates a Fragment with ReactNativeActivity, you can use it as a parameter in the startActivity method in order to push a new activity with embedded React Native.
| Param | Required | Type | Description |
|---|---|---|---|
| moduleName | Yes | String | Name of React Native component registered to AppRegistry. |
| initialProps | No | Bundle || HashMap<String, *> || ReadableMap | Initial properties to be passed to React Native component. |
Examples:
You can easily wrap the ReactNativeFragment inside an AndroidFragment composable to integrate React Native into your Jetpack Compose application. Since the AndroidFragment itself acts as a factory for the given Fragment class, you can pass the required arguments using a Bundle.
The arguments passed to the AndroidFragment match the ones that can be passed to the ReactNativeFragment.createReactNativeFragment factory, yet need to be packed inside a Bundle, as follows:
String under the ReactNativeFragmentArgNames.ARG_MODULE_NAME (equivalent to "arg_module_name") constantBundle under the ReactNativeFragmentArgNames.ARG_LAUNCH_OPTIONS (equivalent to "arg_launch_options") constantYou can find an example app here.