[英]How to call function inside EventHandler function react native
I implement this library https://github.com/elanic-tech/react-native-paytm in my project. 我在我的项目中实现了这个库https://github.com/elanic-tech/react-native-paytm 。 I successfully get a response from Paytm inside my onPayTmResponse but now I want to send this response to the server but inside this function, I am unable to call any function or state it give me an undefined error.
我在onPayTmResponse中成功从Paytm收到了响应,但是现在我想将此响应发送到服务器,但是在此函数中,我无法调用任何函数或声明它给了我一个未定义的错误。
componentWillMount() {
if(Platform.OS == 'ios'){
const { RNPayTm } = NativeModules
const emitter = new NativeEventEmitter(RNPayTm)
emitter.addListener('PayTMResponse', this.onPayTmResponse)
}else{
DeviceEventEmitter.addListener('PayTMResponse', this.onPayTmResponse)
}
//this.makeRemoteRequest();
}
onPayTmResponse(response) {
// Process Response
// response.response in case of iOS
// reponse in case of Android
var actual = "";
if(Platform.OS == 'ios'){
actual = JSON.parse(response.response);
}else{
actual = JSON.parse(response);
}
console.log(actual, this.state);
if(actual.RESPCODE == "01"){
// Place Order
placeOrder();
}else{
alert(actual.RESPMSG);
this.setState({loading: false, buttonText: 'Continue'});
}
}
Bind your onPayTmResponse
function with this inside componentWillMount
like 将您的
onPayTmResponse
函数与此componentWillMount
内部绑定
componentWillMount() {
this.onPayTmResponse = this.onPayTmResponse.bind(this); // note: we bind onPayTmResponse with this
if(Platform.OS == 'ios'){
const { RNPayTm } = NativeModules
const emitter = new NativeEventEmitter(RNPayTm)
emitter.addListener('PayTMResponse', this.onPayTmResponse)
}else{
DeviceEventEmitter.addListener('PayTMResponse', this.onPayTmResponse)
}
//this.makeRemoteRequest();
}
Now you should be able to call setState within onPayTmResponse
. 现在,您应该可以在
onPayTmResponse
调用setState了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.