0

我已經在我的項目中完成了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> 
); 
} 

回答

0

您必須通過箭頭函數或直接綁定將您的(this)上下文綁定到該函數。所以你會改變你的函數的聲明。

_fbAuth(){ 

_fbAuth =() => { 

OR你會在你的構造函數中添加。

constructor(props) { 
    super(props); 

    this.state = { 
     fblogindone:false, 
    }; 
    this._fbAuth = this._fbAuth.bind(this) 
} 
+0

同時使用'_fbAuth =()=> {'和'this._fbAuth = this._fbAuth.bind(這)'也有同樣的結果'this.setState不是function' –

+0

我回答我自己的問題 –

0

我修復了我自己的錯誤,所以我發佈我的解決方案,希望以後可以幫助其他人。首先,我把函數名改爲_fbAuth =() =>{,然後裏面的子函數返回所有改爲箭頭函數,(result) => {responseInfoCallback = (error, result) =>{,然後this可以在我的facebook登錄回調裏面使用。

_fbAuth =() =>{ 
     // Attempt a login using the Facebook login dialog asking for default permissions and email. 
     LoginManager.logInWithReadPermissions(['public_profile', 'email']).then(
      (result) => { 
      if (result.isCancelled) { 
       alert('Login cancelled'); 
      } else { 
       // Create response callback. 
       const responseInfoCallback = (error, result) =>{ 
       if (error) { 
        console.log(error); 
        alert('Error fetching data: ' + error.toString()); 
       } else { 
        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); 
      } 
     ); 
    }