Reactjs React Native FBSDK onLoginFinished推送到新屏幕
我对使用react-native有些陌生,我正在尝试使用react-native fbsdk通过我的应用程序实现Facebook登录 我已经达到了这样的程度:它将从用户的Facebook个人资料中请求权限,然后返回到应用程序,并显示一条成功警报消息。我想切换到一个新的屏幕,而不是仅显示警报LoginFinished 这是我在主构造函数类中的登录按钮代码:Reactjs React Native FBSDK onLoginFinished推送到新屏幕,reactjs,mobile,react-native,facebook-javascript-sdk,fbsdk,Reactjs,Mobile,React Native,Facebook Javascript Sdk,Fbsdk,我对使用react-native有些陌生,我正在尝试使用react-native fbsdk通过我的应用程序实现Facebook登录 我已经达到了这样的程度:它将从用户的Facebook个人资料中请求权限,然后返回到应用程序,并显示一条成功警报消息。我想切换到一个新的屏幕,而不是仅显示警报LoginFinished 这是我在主构造函数类中的登录按钮代码: constructor(props){ super(props); this.goToNewPage = this.goToNewPage
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.goToNewPage=this.goToNewPage.bind(this);
const infoRequest=新GraphRequest(
“/我”,
{参数:{
字段:{
字符串:“电子邮件,名字,姓氏,id,性别”//你想要得到什么
}
}},
这个._responseInfo回调,
);
Login=React.createClass({
render:function(){
返回(
{
如果(错误){
警报(“登录失败,错误为:+result.error”);
}否则如果(结果被取消){
警报(“登录被取消”);
}否则{
警报(“登录成功,权限:+result.grantedPermissions”)
新建GraphRequestManager().addRequest(infoRequest).start();
这是goToNewPage;
}
}
}
onLogoutFinished={()=>警报(“用户注销”)}
/>
);
}
});
}//关闭构造函数
_ResponseInfo回调(错误:?对象,结果:?对象){
如果(错误){
警报('获取数据时出错:'+Error.toString());
}否则{
警报('成功获取数据:'+result.toString());
控制台日志(结果);
}
}
goToNewPage(){
log(“Hello from go to new page function”);
这个是.props.navigator.push({
id:'新页面',
名称:“转到新页面”,
});
}
但在显示成功警报后,它不会推送到新页面。在我的登录类中似乎无法识别this.props.navigator,我也不知道为什么。任何帮助都将不胜感激事实证明,问题是我在构造函数中声明了login类。将其移动到componentWillMount函数为我修复了它 问题是我在构造函数中声明了login类。将其移动到componentWillMount函数为我修复了它 当前您正在错误地调用goToNewpage()方法,LoginButton组件的结果块,在任何类型的块中,我们都无法直接访问当前类引用“this”,因为它失去了可访问性 然后,您应该为该组件创建一个全局变量,如下所示 var_this 并在组件构造函数中指定类的当前引用,如: _这个=这个
并且应该在goToNewpage()方法中使用_this.props.navigator.push代替this.props.navigator.push。谢谢 当前您正在错误地调用goToNewpage()方法,LoginButton组件的结果块,在任何类型的块中,我们都无法直接访问当前类引用“this”,因为它失去了可访问性 然后,您应该为该组件创建一个全局变量,如下所示 var_this 并在组件构造函数中指定类的当前引用,如: _这个=这个
并且应该在goToNewpage()方法中使用_this.props.navigator.push代替this.props.navigator.push。谢谢 我和你在我的应用程序上做的一样,区别在于我调用了另一个绑定到组件的方法。我没有在这里尝试过,但是如果您创建另一个方法并将其绑定到组件,然后调用navigator,它可能会工作。我在我的应用程序上也会这样做,区别在于我调用了另一个绑定到组件的方法。我没有在这里尝试过,但是如果您创建另一个方法并将其绑定到组件,然后调用导航器,它可能会工作。