问题
I have an code like this:
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);
But I get an error like this: 'null is not an object (evaluating 'ShareDialog.canShow')
here the screenshot
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.
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!
回答1:
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
So Change
ShareDialog.canShow(this.state.shareLinkContent)
To
ShareDialog.canShow(tmp.state.shareLinkContent)
Would fix your issue
来源:https://stackoverflow.com/questions/54696944/null-is-not-an-object-evaluating-sharedialog-canshow