### Simple one-off example #### React ```js import { NativeModules } from 'react-native' const videoPlayer = NativeModules.VideoPlayer videoPlayer.seekTo(100) ``` #### iOS Keep in mind: - `RCT_EXPORT_METHOD` exposes the name of the method up to the __first__ colon in js land. - When many methods share the same name, use `RCT_REMAP_METHOD` to define a different name for js and map it to the native method. ie. `RCT_REMAP_METHOD(differentMethodName, methodName:(BOOL)arg1 arg2:(BOOL)arg2)`. [docs](http://facebook.github.io/react-native/releases/next/docs/native-modules-ios.html#ios-calendar-module-example) ```obj-c // VideoPlayer.h #import @interface VideoPlayer : NSObject @end @implementation VideoPlayer RCT_EXPORT_MODULE(); // or RCT_EXPLORT_MODULE(AwesomeVideoPlayer) -- custom name RCT_EXPORT_METHOD(seekTo:(double)time) { // seek to time } @end ``` #### Android [docs](http://facebook.github.io/react-native/releases/next/docs/native-modules-android.html#the-toast-module) ```java // VideoPlayer.java package com.beme.react import com.facebook.react.bridge.NativeModule; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactMethod; public class VideoModule extends ReactContextBaseJavaModule { public VideoModule(ReactApplicationContext reactContext) { super(reactContext) } @Override public String getName() { return "VideoPlayer"; } @ReactMethod public void seekTo(double time) { // seek to time } } ``` Register the module ```java // VideoPackage.java package com.beme.react import com.facebook.react.ReactPackage; import com.facebook.react.bridge.JavaScriptModule; import com.facebook.react.bridge.NativeModule; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.uimanager.ViewManager; public class VideoPackage implements ReactPackage { @Override public List> createJSModules() { return Collections.emptyList(); } @Override public List createViewManagers(ReactApplicationContext reactContext) { return Collections.emptyList(); } @Override public List createNativeModules( ReactApplicationContext reactContext) { List modules = new ArrayList<>(); modules.add(new VideoModule(reactContext)); return modules; } } ``` ```java // MainApplication.java protected List getPackages() { return Arrays.asList( new MainReactPackage(), new VideoPackage()); // <-- Add this line with your package name. } ``` [__Go to Top__](https://gist.github.com/chourobin/f83f3b3a6fd2053fad29fff69524f91c#file-0-bridging-react-native-cheatsheet-md)