0

私は自分のプロジェクトでFacebookログイン機能を行っていますが、ログイン成功時には私はsetStateできません。このエラーはthis.setState is not a functionです。 setStateに成功した後、別の関数呼び出しを作成しようとしましたが、結果は同じで、私の関数は関数ではないことを伝えています。私は何をすべきか ?React Native - FacebookのログインはsetStateできません

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 =() => { 

OR

_fbAuth(){ 

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 =() =>{に変更し、サブ関数内で矢印関数と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); 
      } 
     ); 
    } 
関連する問題