我做这个类:成员函数 - JavaScript的
import React, {Component} from 'react';
import {
View,
ActivityIndicator,
Button
} from 'react-native';
import Auth from '../api/Auth';
export default class LoginScreen extends React.Component {
constructor(props) {
super(props);
this.state = {
alreadyLoggedIn: true
};
this.redirectToHome.bind(this);
this.loginWithFacebook.bind(this);
}
componentWillMount() {
Auth.isAlreadyLoggedIn().then((res) => {
if(res == true) {
this.redirectToHome();
} else {
this.setState({alreadyLoggedIn: false});
}
});
}
redirectToHome() {
this.props.navigator.resetTo({
screen: 'homeScreen',
title: 'Meetups'
});
}
loginWithFacebook() {
Auth.loginWithFacebook().then((success) => {
if(success == true) {
this.redirectToHome();
}
}).catch((err) => {
console.error(err);
});
}
render() {
return(
<View>
{this.state.alreadyLoggedIn == false &&
<Button title="Login with Facebook" onPress={this.loginWithFacebook} />
}
{this.state.alreadyLoggedIn == true &&
<ActivityIndicator animating={this.state.alreadyLoggedIn} />
}
</View>
);
}
}
里面的loginWithFacebook
功能,当它到达的地方调用redirectToHome
函数的行,就这样说:
_this3.redirectToHome is not a function
我是否以错误的方式访问此函数?我无法使该功能成为静态,并且它需要this
对象。
我该怎么办?
它的工作!但是,为什么当我使用'this.loginWithFacebook.bind(this)'时它会工作?我的意思是在此之后,我可以在loginWithFacebook函数中完成this.props,而不必在构造函数中更新它 –