我已經在我的項目中完成了Facebook登錄功能,但是當登錄成功時,我不能登錄setState
。我得到這個錯誤this.setState is not a function
。我試圖創建一個單獨的函數調用成功後,setState
,結果是一樣的,告訴我我的功能不是一個函數。我該怎麼辦 ?React Native - Facebook登錄無法設置狀態
constructor(props) {
super(props);
this.state = {
fblogindone:false,
};
}
_fbAuth(){
// Attempt a login using the Facebook login dialog asking for default permissions and email.
LoginManager.logInWithReadPermissions(['public_profile', 'email']).then(
function(result) {
if (result.isCancelled) {
alert('Login cancelled');
} else {
// Create response callback.
const responseInfoCallback = function(error, result) {
if (error) {
console.log(error);
alert('Error fetching data: ' + error.toString());
} else {
console.log(JSON.stringify(result));
this.setState({
fblogindone:true
});
}
}
// Create a graph request asking for user email and names with a callback to handle the response.
const infoRequest = new GraphRequest('/me',{
parameters: {
fields: {
string: 'id,name,email'
}
}
},
responseInfoCallback
);
// Start the graph request.
new GraphRequestManager().addRequest(infoRequest).start()
}
},
function(error) {
alert('Login fail with error: ' + error);
}
);
}
render() {
return(
<View style={styles.topcontainer}>
<TouchableOpacity activeOpacity={0.9} onPress={()=> this._fbAuth()}>
<FoIcon name="user-circle-o" size={33} color="#fff"/>
</TouchableOpacity>
</View>
);
}
同時使用'_fbAuth =()=> {'和'this._fbAuth = this._fbAuth.bind(這)'也有同樣的結果'this.setState不是function' –
我回答我自己的問題 –