Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Getting to the Native 
in React Native

Getting to the Native 
in React Native

The Native part in React Native can look very scary and intimidated but is does not have to be that way, so in this talk you will see what the most frequent challenges are when working on the native side of React Native and how to conquer them.

Wouter van den Broek

September 06, 2018
Tweet

Other Decks in Programming

Transcript

  1. @interface RCTVibration : NSObject <RCTBridgeModule> @end @implementation RCTVibration RCT_EXPORT_MODULE() //

    or RCT_EXPORT_MODULE(RCTVibration) RCT_EXPORT_METHOD(vibrate) { AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); } RCTVibration.m Native modules
  2. @ReactModule(name = "Vibration") public class VibrationModule extends ReactContextBaseJavaModule { public

    VibrationModule(ReactApplicationContext reactContext) { super(reactContext); } @Override public String getName() { return "Vibration"; } @ReactMethod public void vibrate(int duration) { Vibrator v = (Vibrator) getReactApplicationContext() .getSystemService(Context.VIBRATOR_SERVICE); if (v != null) { v.vibrate(); } } } Native modules VibrationModule.java
  3. @interface RCTVibration : NSObject <RCTBridgeModule> @end @implementation RCTVibration RCT_EXPORT_MODULE() RCT_EXPORT_METHOD(vibrate)

    { AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); } RCTVibration.m Native modules
  4. @interface RCTVibration : NSObject <RCTBridgeModule> @end @implementation RCTVibration RCT_EXPORT_MODULE() RCT_EXPORT_METHOD(vibrate)

    { if (vibration) { resolve(vibration); } else { reject(@"no_vibration"); } } RCTVibration.m Native modules
  5. @interface RCTVibration : RCTEventEmitter<RCTBridgeModule> @end @implementation RCTVibration RCT_EXPORT_MODULE() - (NSArray<NSString

    *> *)supportedEvents { return @[@"vibrate"]; } RCT_EXPORT_METHOD(vibrate) { [self sendEventWithName:@"vibrationDone" body:@{@"done": true}]; } RCTVibration.m Native modules
  6. @interface RedViewManager : RCTViewManager @end @implementation RedViewManager RCT_EXPORT_MODULE() - (UIView

    *)view { return [UIView new].backgroundColor = UIColor.RedColor; } @end Native modules RedViewManager.m
  7. import { requireNativeComponent } from 'react-native'; const redView = requireNativeComponent(‘RedView',

    null); render() { return ( <RedView /> ); } Native modules RedView.js
  8. - (NSDictionary *)constantsToExport { return @{ @"shake": @"off" }; }

    Native modules RCTVibration.m const RCTVibration = require('NativeModules').Vibration; const Shake = RCTVibration.shake;
  9. @objc(Vibration) class Vibration: NSObject { @objc(vibrate) func vibrate() -> Void

    { AudioServicesPlaySystemSound.kSystemSoundID_Vibrate } @objc func constantsToExport() -> [String: Any]! { return ["shake": "off"] } } Native modules RCTVibration.Swift
  10. dependencies { … classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.30" } apply plugin: "kotlin-android" apply

    plugin: "kotlin-android-extensions" dependencies { … compile "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.30" } Native modules build.gradle
  11. class VibrationModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) { override fun getName(): String

    { return "Vibration" } @ReactMethod fun vibrate() { val v = reactApplicationContext.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator v.vibrate() } } Native modules VibrationModule.kt
  12. public class ReactActivity public class MainActivity extends ExampleReactActivity public ReactActivityDelegate

    public class ExampleReactActivityDelegate Bundle getLaunchOptions() { Bundle options = new Bundle(); options.putString("googleKey", "12345"); return options; } Extending native modules ExampleReactActivity.java ExampleReactActivityDelegate.java
  13. class App extends React.Component { constructor(props){ super(props); // use the

    prop: prop.googleKey } } Extending native modules App.js
  14. subprojects { afterEvaluate { project -> if (project.hasProperty("android")) { android

    { compileSdkVersion 27 buildToolsVersion '27.0.3' } } } } Building build.gradle
  15. implementation(project(':react-native-onesignal')) { exclude group: 'com.google.firebase } implementation(project(':react-native-onesignal')) { exclude group:

    'com.google.firebase', module: 'firebase-messaging' } implementation ('com.google.firebase:firebase-messaging:12.0.1') { force = true; } Building build.gradle
  16. subprojects { project.configurations.all { resolutionStrategy.eachDependency { details -> if (details.requested.group

    == 'com.android.support' && !details.requested.name.contains('multidex')) { details.useVersion "27.1.1" } } } } Building build.gradle
  17. allprojects { repositories { ... configurations.all { resolutionStrategy { force

    ‘com.google.android.gms:play-services-gcm:12.0.0’ } } } } Building build.gradle
  18. "xcodeSchemes": { "Debug": ["Debug", "DebugFree"], "Release": ["Release", "ReleaseFree"] } "scripts":

    { "postinstall": "react-native-schemes-manager all" } Building package.json