React Native App Development Company in USA | Page 5
and call that method from JavaScript code. we'll create this method as a callback.
Let's update the code in bold in Bulb.swift
Import Foundation
@oblc(Bulb)
Class Bulb :NSObject {
@objc
Static var isOn = false
@objc
Func turnOn () {
Bulb.isOn= true
Print (“Bulb is now ON”)
}
@objc
Func turnOFF(){
Bulb.isOn = false
Print (“”print Bulb is now OFF”)
}
@objc
Func getStatus(_ callback : RCTResponseSenderBlock){
callback ([NSNull(),Bulb.isOn])
}
@objc
Static func requireMainQueueSetup() -> {
return true
}
}
COPY
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;