When you want to reuse code written in a native language - Using a third party library only available in native language - High performance, multi-threaded code (image processing, for example) Why use it? 5
Export constants • Export and implement view • Export view properties • Export view methods • Export events • Create your javascript library as a wrapper for the native module you just implemented, export the module and its methods and use it in your app
RCTBridgeModule protocol. ◦ Use macro to export module • Android ◦ Create Package ◦ Make a class extending ReactContextBaseJavaModule. ◦ Implement getName() method.
RCT_EXPORT_METHOD • Android ◦ use @ReactMethod directives • takePicture example in RNCamera ◦ receives a map of options, a number called reactTag and the promise to be resolved/rejected ◦ reactTag is the key to the module view in the native view hierarchy map.
your RN wrapper render them • Useful when you have a view written in native code ◦ RNCamera: our view is the camera preview ◦ Other example: Visa Checkout SDK ▪ Visa checkout button with swiper • Views can have props like usual react components ◦ RNCamera has props like flashMode, type (front or back), white balance and many other
extend UIView • layoutSubviews is a method of the UIView lifecycle called by iOS when laying our view. • We use this opportunity to force our preview layer to be the whole view, by setting our frame's layer to the value of our bounds
the View class • onLayout is a method of Android view lifecycle called when the view is ready to be displayed • We use this opportunity to set the size of our preview layer
extend RCTViewManager and have a view method (it will omit the Manager from the name automatically when searching). ◦ Android, will look for ViewManager with that name
holds a reference to a RCTDirectEventBlock, which can be used to send events back to JS. • You can return a NSDictionary, which will be converted to a JS object, and each value inside the dictionary will be converted to its correspondant JS type.
getExportedCustomDirectEvent() to export them to JS • Use react event library to dispatch event to JS • Map is also converted to JS object and to JS types.
React Component • If your native module does not have a view, you can only export methods and constants • Access your methods and constants with your manager/module (CameraManager) • If you have a view, implement the react component render and lifecycle as normal and render your native component you required with the requireNativeComponent() method
You need to implement the same logic twice • Bridge can be slow to pass big amounts of data (like images as base64 strings) • Need to be familiar with other IDEs like XCode and Android Studio • Android