我目前使用React Native 0.39.2 w/newest TypeScript,當我運行我的componentDidMount()方法和setState時,我得到一個錯誤this.setState不是一個函數。React Native w/TypeScript this.setState不是函數
我試着用 this.setState({isLoggedIn: true}).bind(this)
結合,雖然因爲我使用的是布爾作爲類型它不會讓我沒有給一個類型的錯誤,甚至設置爲任何類型仍然得到同樣的錯誤。
這裏是我的代碼先用我的接口國家
import React, {
Component
} from 'react';
import { AppRegistry, View, StyleSheet, Text } from 'react-native';
import { Actions } from 'react-native-router-flux';
import Firestack from 'react-native-firestack';
const firestack = new Firestack();
interface Props {
}
interface State {
isLoggedIn?: boolean;
}
export default class MainList extends Component<Props, State> {
state = {
isLoggedIn: false,
};
constructor(props: any) {
super(props);
console.log("Is anyone logged in?: " + this.isLoggedIn);
}
isLoggedIn = this.state.isLoggedIn;
componentDidMount() {
if (!this.isLoggedIn) {
Actions.welcome();
}
firestack.auth.listenForAuth(function(evt: any) {
// evt is the authentication event
// it contains an `error` key for carrying the
// error message in case of an error
// and a `user` key upon successful authentication
if (!evt.authenticated) {
// There was an error or there is no user
//console.error(evt.error);
this.setState({isLoggedIn: false});
console.log("The state of isLoggedIn is: " + this.isLoggedIn);
} else {
// evt.user contains the user details
console.log('User details', evt.user);
this.setState({isLoggedIn: true});
console.log("The state of isLoggedIn is: " + this.isLoggedIn);
}
});
}
render() {
return (
<View style={styles.View}>
<Text style={styles.textLabel}>The main view</Text>
</View>
)
}
}
const styles = StyleSheet.create({
View: {
padding: 20
},
textLabel: {
fontSize: 20,
marginBottom: 10,
height: 20
},
textInput: {
height: 20,
fontSize: 15,
marginBottom: 20
}
});
AppRegistry.registerComponent('MainList',()=> MainList);
有什麼我錯過這裏?
發佈您的整個文件。你如何宣佈你的班級? – bluetoft
看起來像有很多東西你失蹤 – Maxwelll