08.10.2020 Views

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.

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

@objc

Func getStatus(_ callback : RCTResponseSenderBlock){

callback

([NSNull(),Bulb.isOn])

}

@objc

Static func requireMainQueueSetup() -> {

return true

}

}

getStatus() method receives a callback parameter that we'll pass from your JavaScript code. and that we have called the

callback with an array of values, which can be exposed in JavaScript. we've passed NSNull() because the first element,

which we've considered as a mistake within the callback.

And then finally update the React Code:

Import React , {Component} from ‘react;

Import {Text,View,NativeModules,TouchableOpacity} from ‘react-native;

Export default class App extends Component {

constructor(props){

super(props);

This.state = {isOn : false};

this.updateStatus();

}

turnOn =() => {

NativeModules.Bulb.turnOn();

this.updateStatus();

updateStatus = () => {

NativeModules.Bulb.getstatus((error,isOn ) => {

this.setState({ isOn : isOn)};

)}

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!