【问题标题】:null is not an object (evaluating 'ShareDialog.canShow')null 不是对象(评估“ShareDialog.canShow”)
【发布时间】:2019-02-14 18:31:29
【问题描述】:

我有这样的代码:

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')

here the screenshot

我正在使用本机反应。我不明白为什么我会收到这个错误。我使用的 React Native 和 React 最新版本。我为我的应用做了 Facebook SDK 设置。

我测试了不止一次,但仍然出现错误。顺便说一下,为什么 stackoverflow 会说“看起来你的帖子主要是代码;请添加更多细节。”很烦人!

【问题讨论】:

    标签: reactjs react-native


    【解决方案1】:

    您在 shareLinkWithShareDialog 函数中将此引用为局部变量,但为了 canShow 您正在使用 this.state.shareLinkContent 传递状态,这是不正确的。应该是 tmp.state.shareLinkContent

    所以改变

        ShareDialog.canShow(this.state.shareLinkContent)
    

        ShareDialog.canShow(tmp.state.shareLinkContent)
    

    会解决你的问题

    【讨论】:

    • 整合我的答案中引入的更改后,现在有什么问题?
    • 它仍然给出同样的错误:'null is not an object (evalating 'ShareDialog.canShow')
    • 这个答案不起作用......我也尝试过并得到同样的错误。
    猜你喜欢
    • 1970-01-01
    • 2022-01-05
    • 2021-10-20
    • 2016-06-08
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多