React-Native 错误 this.setState 不是函数
React-Native error this.setState is not a function
我正在使用下面的库为每个 ApiRequest 实现回调(onSuccess、onError)。但是我在触发事件时更新状态时遇到问题。我试图删除所有内容,只保留基本逻辑。我不知道为什么会出错。
图书馆:https://www.npmjs.com/package/react-native-simple-events
下面是我的代码
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
方法上绑定它:
Events.on('LoginSuccess', 'myID', this.onLoginSuccess.bind(this));
我正在使用下面的库为每个 ApiRequest 实现回调(onSuccess、onError)。但是我在触发事件时更新状态时遇到问题。我试图删除所有内容,只保留基本逻辑。我不知道为什么会出错。 图书馆:https://www.npmjs.com/package/react-native-simple-events
下面是我的代码
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
方法上绑定它:
Events.on('LoginSuccess', 'myID', this.onLoginSuccess.bind(this));