0
我正在使用下面的lib为每个ApiRequest实现一个回调(onSuccess,onError)。但是,当触发事件时更新状态时遇到问题。我试图删除所有的东西只是保持基本的逻辑。我不知道它为什么错误。 库:https://www.npmjs.com/package/react-native-simple-eventsReact-Native错误this.setState不是函数
下面是我的代码
ApiRequest.js
import Events from 'react-native-simple-events';
export function login(email, password) {
Events.trigger('LoginSuccess', 'response');
}
Login.js
import React, { Component, } from 'react'
import {
View,
Text,
} from 'react-native'
import Events from 'react-native-simple-events';
import * as request from '../../network/ApiRequest'
class LoginScreen extends Component {
static propTypes = {}
static defaultProps = {}
constructor(props) {
super(props)
this.state = {
status: "new"
}
}
componentDidMount() {
Events.on('LoginSuccess', 'myID', this.onLoginSuccess);
request.login("abc","def")
}
componentWillUnmount() {
Events.rm('LoginSuccess', 'myID');
}
onLoginSuccess(data){
this.setState({ //=>error here
status : "done"
});
}
render() {
return (
<View>
<Text>
{this.state.status}
</Text>
</View>
)
}
}
让我知道如果你需要更多的信息
你是否在'onLoginSuccess'函数中检查'this'是否可以访问?尝试在'onLoginSuccess'函数内打印'this'。如果这是不可访问的,那么当你打电话给'onLoginSuccess'时,你需要绑定'this'''('LoginSuccess','myID',this.onLoginSuccess.bind(this)); ' – iamsaksham