React Native Brownfield provides first-class support for Java.
Even though the library provides first-class Java support, it's written in Kotlin. To compile the project with React Native Brownfield, we need to make sure that our project's build.gradle file contains the required Gradle plugin.
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 DefaultHardwareBackBtnListener 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:
getSharedA 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.
| 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 find an example app here.