2017-01-08 18 views
1

我有點新使用反應原生,我試圖實現Facebook登錄與我的應用程序使用反應原生fbsdk。反應本機FBSDK onLoginFinished推到新屏幕

我已經知道它會向用戶的Facebook配置文件請求權限,然後返回到應用並顯示成功提醒消息。我不想在登錄已完成時顯示警報,而是想推入新的屏幕。

這是在主類的構造函數我的登錄按鈕代碼:

constructor(props){ 
super(props); 
this.goToNewPage = this.goToNewPage.bind(this); 
const infoRequest = new GraphRequest(
    '/me', 
    {parameters: { 
      fields: { 
      string: 'email,first_name,last_name,id,gender' // what you want to get 
      } 
    }}, 
    this._responseInfoCallback, 
); 
Login = React.createClass({ 
    render: function() { 
    return (
     <View > 
     <LoginButton 
      navigator={this.props.navigator} 
      style = {{marginLeft:55, marginRight:55, height:50, width:300}} 

      onLoginFinished={ 
      (error, result) => { 
       if (error) { 
       alert("Login failed with error: " + result.error); 
       } else if (result.isCancelled) { 
       alert("Login was cancelled"); 
       } else { 
       alert("Login was successful with permissions: " + result.grantedPermissions) 
       new GraphRequestManager().addRequest(infoRequest).start(); 
       this.goToNewPage; 

       } 
      } 
      } 
      onLogoutFinished={() => alert("User logged out")} 
      /> 
     </View> 
    ); 
    } 
}); 
} //closes constructor 

_responseInfoCallback(error: ?Object, result: ?Object) { 
if (error) { 
    alert('Error fetching data: ' + error.toString()); 
} else { 
    alert('Success fetching data: ' + result.toString()); 
    console.log(result); 
} 
} 

goToNewPage(){ 
console.log("Hello from go to new page function"); 
this.props.navigator.push({ 
    id: 'newPage', 
    name: 'Going To New Page', 
}); 
} 

但都顯示成功後的警報,並沒有推到了新的一頁。似乎this.props.navigator在我的Login類中不被識別,我不知道爲什麼。任何幫助都將不勝感激

+0

我在你的app上做的和你一樣,區別在於我調用另一個綁定到組件的方法。我沒有在這裏試過,但如果你創建另一個方法並將它綁定到組件,然後調用導航器,它可能會工作。 –

回答

0

原來的問題是,我在我的構造函數中聲明登錄類。將它移動到componentWillMount函數爲我修復了它!

0

當前您在調用goButtonWith()方法時出錯,LoginButton組件的結果塊以及任何類型的塊中,我們無法直接訪問當前類引用「this」,因爲它失去了可達性。

那麼你應該創建一個全局變量,這個組件

VAR _this;

和在像部件構造分配類的電流參考:

_this =此;

並且應該使用_this.props.navigator.push代替goToNewpage()方法中的this.props.navigator.push。謝謝!