:メンバ関数未定義 - 私はこのクラスを作っているの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>
);
}
}
それはredirectToHome
関数を呼び出す行に到達loginWithFacebook
機能、内部では、それはこのことを言う:
_this3.redirectToHome is not a function
私は間違った方法でこの機能にアクセスしていますか?関数を静的にすることはできず、this
オブジェクトが必要でした。
どうすればよいですか?
それは働きました!しかし、 'this.loginWithFacebook.bind(this)'だけを使うと、なぜうまくいくのですか?私はこの後、このことをやり遂げることができます。loginWithFacebook関数内のthis.propsをコンストラクタで更新せずに実行することができます –