我试图在功能组件中构建facebook-signIN,但面临以下错误:错误-对象作为React子对象无效(找到:具有键{data}的对象(。如果要渲染子对象的集合,请改用数组。
以下是代码片段:
从"React"导入React,{Fragment,useEffect,useState};导入{安全区域视图、滚动视图、状态栏、样式表、文本、可触摸不透明度、视图、图像、按钮}来自"react native";从"react native fbsdk"导入{AccessToken,LoginButton};
const App = () => {
const [loggedIn, setLoggedIn] = useState(false);
const [fbUserInfo, setFbUserInfo] = useState({data: ''});
return (
<Fragment>
<StatusBar barStyle="dark-content" />
<SafeAreaView>
<ScrollView contentInsetAdjustmentBehavior="automatic"
style={styles.scrollView}>
<View style={styles.body}>
<View style={styles.sectionContainer}>
<LoginButton
onLoginFinished={(error, result) => {
if (error) {
console.log('login has error: ' + result.error);
} else if (result.isCancelled) {
console.log('login is cancelled.');
} else {
console.log(result);
AccessToken.getCurrentAccessToken().then((data) => {
//setLoggedIn(true);
setFbUserInfo(data.userID);
console.log("Login", data, data.accessToken.toString());
});
}
}}
onLogoutFinished={() => {
//setLoggedIn(false);
setFbUserInfo({data:''});
}}
/>
</View>
<View style={styles.buttonContainer}>
{!loggedIn && (
<Text>You are currently logged out</Text>
)}
</View>
{(
<View>
<View style={styles.listHeader}>
<Text>User Info</Text>
</View>
<View style={styles.detailContainer}>
<Text style={styles.title}>ID</Text>
<Text style={styles.message}>{fbUserInfo}</Text>
</View>
</View>
)}
</View>
</ScrollView>
</SafeAreaView>
</Fragment>
);
};
export default App;
请帮助我解决问题,谢谢
试试这个
AccessToken.getCurrentAccessToken().then((data) => {
const { accessToken } = data;
fetch('https://graph.facebook.com/v2.5/me?fields=email,name,friends&access_token=' + accessToken)
.then((response) => response.json())
.then((json) => {
// Some user object has been set up somewhere, build that user here
const userID = json.id;
//setLoggedIn(true);
setFbUserInfo({data: userID});
})
.catch(() => {
reject('ERROR GETTING DATA FROM FACEBOOK')
});
});
我用这个解决了它;
const [fbUserInfo, setFbUserInfo] = useState('');
onLogoutFinished={() => {
setLoggedIn(false);
setFbUserInfo('');
}}