React Native App Development Company in USA | Page 8
@Override
public string getName (){
Return : ‘Bulb’;
}
}
COPY
We have created a Bulb Java class which is rooted from ReactContextBaseJavaModule. ReactContextBaseJavaModule
requires that a function called getName is usually implemented. The aim of this method is to return the string name of
the Native Module which represents this class in JavaScript. So here we'll call this Bulb in order that we will access it
through React.NativeModules.Bulb in JavaScript. rather than Bulb, we will have a special name also.
Not all functions are exposed to Javascript explicitly, to show a function to JavaScript a Java method must be annotated
using @ReactMethod. The return sort of bridge method is usually void.
We have also created a getStatus function which has params as callback and it returns a callback and passes the worth
of static variable isOn.
Next step is to register the module, if a module isn't registered it'll not be available from JavaScript. Create a file by
clicking on Menu File -> New -> Java Class and therefore the filename as BulbPackage then click OK. then add following
code to
package com.bridgeapp;
Import com.facebook.react.bridge.Callback;
Import com.facebook.react.bridge.ReactApplicationContext;
Import com.facebook.react.bridge.ReactContextBaseJavaModule;
Import com.facebook.react.bridge.ReactMethod;
class Bulb extends ReactContextBaseJavaModule {
Private static Boolean inOn = false;
Public Bulb (ReactApplicationContext reactContext) {
Super (reactContext);
}
@ReactMethod
Public void getStatus (
Callback successCallback){
successCallback.invoke ( null,isOn);
}
@ReactMethod
Public void turnOn (){
isOn = true ;