null 不是对象(评估 'ShareDialog.canShow')



我有这样的代码:

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 Native和React我使用的最新版本。我为我的应用程序进行了Facebook SDK设置。

我测试了多次以上,但仍有错误。顺便说一句,为什么Stackoverflow说"看来您的帖子主要是代码;请添加更多详细信息。"这很烦人!

您将其转介给sharelinkwithsharedialog函数中的局部变量,但要使用this.state.state.sharelinkcontent传递状态,这是不正确的。它应该是tmp.state.sharelinkcontent

所以更改

    ShareDialog.canShow(this.state.shareLinkContent)

to

    ShareDialog.canShow(tmp.state.shareLinkContent)

将解决您的问题

相关内容

  • 没有找到相关文章

最新更新