我正在制作一个带有facebook登录的本地ios应用程序,现在需要在所有其他屏幕上检索结果数据。
我有树幕:
登录包含工作的loginButton女巫,并返回控制台中的用户public_profile。
Main包含一个堆栈导航器并导入其他屏幕。
配置文件是我想打印的登录用户名,img等。
问题是,当onLoginFinished成功时,它会返回用户信息,但是如何在其他屏幕上获取它呢?
看上去这是我需要的GraphRequestManager和infoRequest,但却想不出怎么做。
Main.js
'use-strict';
var React = require('react');
window.React = React;
import {AppRegistry, Text} from 'react-native';
import { StackNavigator } from 'react-navigation';
//login screen
import Login from './Login'
//profile screen
var Profile = require('./Profile.js');
const skatebayApp = StackNavigator({
Login: {screen: Login},
Profile: {screen: Profile},
},{
headerMode: 'screen',
});
AppRegistry.registerComponent('skatebayApp', () => skatebayApp);Login.js
'use-strict';
import React, { Component } from 'react';
import {
Text,
View,
TouchableHighlight,
Image,
} from 'react-native';
const FBSDK = require('react-native-fbsdk');
const {
LoginButton,
AccessToken,
GraphRequest,
GraphRequestManager,
} = FBSDK;
export default class Login extends Component{
render(){
return(
<LoginButton
onLoginFinished={
(error, result) => {
if (error) {
alert("login has error: " + result.error);
} else if (result.isCancelled) {
alert("login is cancelled.");
} else {
AccessToken.getCurrentAccessToken().then((data) => {
let accessToken = data.accessToken
const responseInfoCallback = (error, result) => {
if (error) {
console.log(error)
alert('Error fetching data: ' + error.toString());
} else {
console.log(result)
}
}
const infoRequest = new GraphRequest(
'/me',
{
accessToken: accessToken,
parameters: {
fields: {
string: 'email,name,first_name,last_name,picture'
}
}
},
responseInfoCallback,
console.log(result)
);
new GraphRequestManager().addRequest(infoRequest).start()
})
}
}
} onLogoutFinished={() => console.log('signed out')}/>
);
}
}Profile.js -我想在这里获取用户信息并打印出来
'use-strict';
import React, { Component } from 'react';
import {
AppRegistry,
Text,
View,
Image,
} from 'react-native';
import { StackNavigator } from 'react-navigation';
class Profile extends React.Component {
render() {
return (
<View>
<Text>Username & other info here</Text>
</View>
);
}
}
module.exports = Profile;希望这有什么意义,我对本族语的反应很新鲜。
发布于 2017-05-10 10:21:42
将用户详细信息存储在异步存储中,然后可以从任何屏幕访问它。您可以在这里找到更多详细信息,https://facebook.github.io/react-native/docs/asyncstorage.html。
https://stackoverflow.com/questions/43886128
复制相似问题