|
| 1 | +## Objective-C |
| 2 | + |
| 3 | +React Native Brownfield provides first-class support for Java. |
| 4 | + |
| 5 | +### Linking |
| 6 | + |
| 7 | +The library is meant to work with [auto linking](https://github.com/react-native-community/cli/blob/master/docs/autolinking.md). In case you can't use this feature, please check out the following options: |
| 8 | + |
| 9 | +<details> |
| 10 | +<summary>react-native link</summary> |
| 11 | +Run the following command in your terminal: |
| 12 | + |
| 13 | +```bash |
| 14 | + react-native link @callstack/react-native-brownfield |
| 15 | +``` |
| 16 | +</details> |
| 17 | + |
| 18 | +<details> |
| 19 | +<summary>Manually link the library on Android</summary> |
| 20 | + |
| 21 | +#### `android/settings.gradle` |
| 22 | +```groovy |
| 23 | +include ':react-native-brownfield' |
| 24 | +project(':react-native-brownfield').projectDir = new File(rootProject.projectDir, '../node_modules/@callstack/react-native-brownfield/android') |
| 25 | +``` |
| 26 | + |
| 27 | +#### `android/app/build.gradle` |
| 28 | +```groovy |
| 29 | +dependencies { |
| 30 | + ... |
| 31 | + implementation project(':react-native-brownfield') |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +#### `android/app/src/main/.../MainApplication.java` |
| 36 | +On top, where imports are: |
| 37 | + |
| 38 | +```java |
| 39 | +import com.callstack.reactnativebrownfield.ReactNativeBrownfieldPackage; |
| 40 | +``` |
| 41 | + |
| 42 | +Add the `ReactNativeBrownfieldPackage` class to your list of exported packages. |
| 43 | + |
| 44 | +```java |
| 45 | +@Override |
| 46 | +protected List<ReactPackage> getPackages() { |
| 47 | + return Arrays.asList( |
| 48 | + new MainReactPackage(), |
| 49 | + new ReactNativeBrownfieldPackage() |
| 50 | + ); |
| 51 | +} |
| 52 | +``` |
| 53 | +</details> |
| 54 | + |
| 55 | +### API Reference |
| 56 | + |
| 57 | +#### ReactNativeBrownfield |
| 58 | + |
| 59 | +You can import the object from: |
| 60 | + |
| 61 | +```java |
| 62 | + import com.callstack.reactnativebrownfield.ReactNativeBrownfield; |
| 63 | +``` |
| 64 | + |
| 65 | +--- |
| 66 | + |
| 67 | +**Statics:** |
| 68 | + |
| 69 | +`initialize` |
| 70 | + |
| 71 | +A function used to initialize a React Native Brownfield singleton. Keep in mind that it **doesn't** load React Native bundle. |
| 72 | + |
| 73 | +Params: |
| 74 | + |
| 75 | +| Param | Required | Type | Description | |
| 76 | +| ----------------------- | -------- | -------------------- | ------------------------------------------------------- | |
| 77 | +| application | Yes | Application | Main application. | |
| 78 | +| rnHost | No* | ReactNativeHost | A instance of [ReactNativeHost](https://bit.ly/2ZnwgnA). | |
| 79 | +| packages | No* | List<ReactPackage> | List of your React Native Native modules. | |
| 80 | +| options | No* | HashMap<String, Any> | Map of initial options. __Options listed below.__ | |
| 81 | + |
| 82 | +> * - Those fields aren't itself required, but at least one of them is. See examples below. |
| 83 | +
|
| 84 | +Available options: |
| 85 | +- useDeveloperSupport: Boolean - Flag to use dev support. |
| 86 | +- packages: List<ReactPackage> - List of your React Native Native modules. |
| 87 | +- mainModuleName: String - Path to react native entry file. |
| 88 | + |
| 89 | +Examples: |
| 90 | + |
| 91 | +```java |
| 92 | + private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) { |
| 93 | + @Override |
| 94 | + public boolean getUseDeveloperSupport() { |
| 95 | + return BuildConfig.DEBUG; |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + protected List<ReactPackage> getPackages() { |
| 100 | + @SuppressWarnings("UnnecessaryLocalVariable") |
| 101 | + List<ReactPackage> packages = new PackageList(this).getPackages(); |
| 102 | + // Packages that cannot be autolinked yet can be added manually here, for example: |
| 103 | + // packages.add(new MyReactNativePackage()); |
| 104 | + return packages; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + protected String getJSMainModuleName() { |
| 109 | + return "index"; |
| 110 | + } |
| 111 | + }; |
| 112 | + |
| 113 | + ReactNativeBrownfield.initialize(this, mReactNativeHost); |
| 114 | +``` |
| 115 | + |
| 116 | +```java |
| 117 | + List<ReactPackage> packages = new PackageList(this).getPackages(); |
| 118 | + |
| 119 | + ReactNativeBrownfield.initialize(this, packages); |
| 120 | +``` |
| 121 | + |
| 122 | +```java |
| 123 | + List<ReactPackage> packages = new PackageList(this).getPackages(); |
| 124 | + HashMap<String, Object> options = new HashMap<>(); |
| 125 | + options.put("packages", packages); |
| 126 | + options.put("mainModuleName", "example/index"); |
| 127 | + |
| 128 | + ReactNativeBrownfield.initialize(this, options); |
| 129 | +``` |
| 130 | + |
| 131 | +--- |
| 132 | + |
| 133 | +`getShared` |
| 134 | + |
| 135 | +A singleton that keeps an instance of ReactNativeBrownfield object. |
| 136 | + |
| 137 | +Examples: |
| 138 | + |
| 139 | +```java |
| 140 | + ReactNativeBrownfield.getShared() |
| 141 | +``` |
| 142 | + |
| 143 | +--- |
| 144 | + |
| 145 | +**Properties:** |
| 146 | + |
| 147 | +| Property | Type | Default | Description | |
| 148 | +| -------------------- | --------- | -------------- | -------------------------------------------------- | |
| 149 | +| bridge | RCTBridge | nil | Launch options, typically passed from AppDelegate. | |
| 150 | +| entryFile | NSString | index | Path to JavaScript root. | |
| 151 | +| fallbackResource | NSString | nil | Path to bundle fallback resource. | |
| 152 | +| bundlePath | NSString | main.jsbundle | Path to bundle fallback resource. | |
| 153 | + |
| 154 | +--- |
| 155 | + |
| 156 | +**Methods:** |
| 157 | + |
| 158 | +`startReactNative` |
| 159 | + |
| 160 | +Starts React Native, produces an instance of a bridge. You can use it to initialize React Native in your app. |
| 161 | + |
| 162 | +Params: |
| 163 | + |
| 164 | +| Param | Required | Type | Description | |
| 165 | +| ----------------------- | -------- | ------------- | ----------------------------------------------------- | |
| 166 | +| startReactNative | No | Lambda | Callback invoked after JS bundle is fully loaded. | |
| 167 | + |
| 168 | +Examples: |
| 169 | + |
| 170 | +```java |
| 171 | + ReactNativeBrownfield.getShared().startReactNative(); |
| 172 | +``` |
| 173 | + |
| 174 | +```java |
| 175 | + ReactNativeBrownfield.getShared().startReactNative(init -> { |
| 176 | + Log.d("loaded", "React Native loaded"); |
| 177 | + }); |
| 178 | +``` |
| 179 | + |
| 180 | +--- |
| 181 | + |
| 182 | +### Linking |
| 183 | + |
| 184 | +You can find an example app [here](../example/java). |
| 185 | + |
| 186 | + |
0 commit comments