我有这样的代码:
import React, { Component } from 'react';
import {AppRegistry, StyleSheet, Text, TouchableHighlight, View,} from 'react-native';
import {LoginButton, ShareDialog} from 'react-native-fbsdk';
class RNSample extends Component {
constructor(props) {
super(props);
const shareLinkContent = {
contentType: 'link',
contentUrl: 'https://www.facebook.com/',
contentDescription: 'Facebook sharing is easy!'
};
this.state = {shareLinkContent: shareLinkContent,};
}
shareLinkWithShareDialog() {
var tmp = this;
ShareDialog.canShow(this.state.shareLinkContent).then(
function(canShow) {
if (canShow) {
return ShareDialog.show(tmp.state.shareLinkContent);
}
}
).then(
function(result) {
if (result.isCancelled) {
alert('Share cancelled');
} else {
alert('Share success with postId: ' + result.postId);
}
},
function(error) {
alert('Share fail with error: ' + error);
}
);
}
render() {
return (
<View style={styles.container}>
<LoginButton
onLoginFinished={
(error, result) => {
if (error) {
alert("Login failed with error: " + error.message);
} else if (result.isCancelled) {
alert("Login was cancelled");
} else {
alert("Login was successful with permissions: " + result.grantedPermissions)
}
}
}
onLogoutFinished={() => alert("User logged out")}/>
<TouchableHighlight onPress={this.shareLinkWithShareDialog.bind(this)}>
<Text style={styles.shareText}>Share link with ShareDialog</Text>
</TouchableHighlight>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
shareText: {
fontSize: 20,
margin: 10,
},
});
AppRegistry.registerComponent('RNSample', () => RNSample);
但是我收到这样的错误:'null is not an object (evaluating 'ShareDialog.canShow')
我正在使用本机反应。我不明白为什么会收到此错误。我使用的React Native和React最新版本。我为我的应用做了Facebook SDK设置。
我测试了不止一次,但仍然出错。顺便说一句,为什么stackoverflow会说“看起来您的帖子主要是代码;请添加更多详细信息。”真烦人!
答案 0 :(得分:0)
您在shareLinkWithShareDialog函数中将其引用到了局部变量,但是在canShow中使用不正确的this.state.shareLinkContent传递状态。它应该是tmp.state.shareLinkContent
所以改变
ShareDialog.canShow(this.state.shareLinkContent)
收件人
ShareDialog.canShow(tmp.state.shareLinkContent)
将解决您的问题