How to Create Bridge in React Native for iOS and Android?
React Native is developed in such a way that we will create a bridge between the language and therefore the JavaScript code. A “bridge” is nothing but how to line up communication between native platform and React Native.
React Native is developed in such a way that we will create a bridge between the language and therefore the JavaScript code. A “bridge” is nothing but how to line up communication between native platform and React Native.
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
}
render(){
return(
<View>
Welcome to Light App !!
Bulb is {this.state.isOn ? “ON” : “OFF”}
{!this.state.isOn ? <TouchableOpacity>
<Text style = {{color : “#FF6347”}}> “Turn ON”
</Text>
</TouchableOpacity>:
<TouchableOpacity>
<Text style ={{color :”#FF6347}}>”Turn OFF”
</Text>
</TouchableOpacity> } </View>
);
}
}
Rebuild the code and run the app, you'll see the Bulb Status value and once you click on activate then it'll show Bulb is
ON
Do remember to rebuild the code rather than refresh, as we changed Native Code.
Section 2 — Native Bridge in Android
In this section, we'll make an equivalent Javascript code which we return for iOS to figure with Android. At this point
we'll create a Bulb class in Java and expose an equivalent function turnOn, TurnOff and getStatus to Javascript.
Open Android Studio and click on Open an existing Android Studio project then select the android folder inside our
LightApp. Once all gradle dependency is downloaded, create a Java Class Bulb.java as shown:
And update the subsequent code in Bulb.java:
Import com.facebook.react.bridge.Callback;
Import com.facebook.react.bridge.ReactApplicationContext;
Import com.facebook.react.bridge.ReactContextBaseJavaModule;
Import com.facebook.react.bridge.ReactMethod;