问题描述
我有这样的代码:
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.
I'm using react native. I don't understand why I get this error. React Native and React latest version I use. I did the Facebook SDK settings for my app.
我测试了不止一次,但仍然出错.顺便说一下,为什么 stackoverflow 会说看起来您的帖子主要是代码;请添加更多详细信息."很烦人!
I tested more than one times but it gets still error. And by the way, why stackoverflow says "It looks like your post is mostly code; please add some more details." It's very annoying!
推荐答案
您在 shareLinkWithShareDialog 函数中将此引用到了局部变量,但是为了 canShow 您正在使用 this.state.shareLinkContent 传递状态,这是不正确的.应该是 tmp.state.shareLinkContent
You referred this to a local variable in shareLinkWithShareDialog function but to canShow you are passing state using this.state.shareLinkContent which isn’t correct. It should be tmp.state.shareLinkContent
所以改变
ShareDialog.canShow(this.state.shareLinkContent)
到
ShareDialog.canShow(tmp.state.shareLinkContent)
会解决您的问题
这篇关于null 不是对象(评估“ShareDialog.canShow")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!