在反应应用程序中集成fb共享功能

时间:2020-10-09 10:51:03

标签: javascript reactjs

我想在我的网站中集成共享功能。根据{{​​3}}文档,对于集成,我需要执行以下操作:

<h1>Sharing using FB.ui() Dialogs</h1>

<p>Below are some simple examples of how to use UI dialogs in a web page.</p>

<h3>Sharing Links</h3>

<div id="shareBtn" class="btn btn-success clearfix">Share Dialog</div>

<p>The Share Dialog enables you to share links to a person's profile without them having to use Facebook Login. <a href="https://developers.facebook.com/docs/sharing/reference/share-dialog">Read our Share Dialog guide</a> to learn more about how it works.</p>

<script>
document.getElementById('shareBtn').onclick = function() {
  FB.ui({
    display: 'popup',
    method: 'share',
    href: 'https://developers.facebook.com/docs/',
  }, function(response){});
}
</script>

我做到了:

function App() {

    const fbstart = () => {
        FB.ui({
            display: 'popup',
            method: 'share',
            href: 'https://developers.facebook.com/docs/',
        }, function (response) {
        });

    }
    return (
        <div className="App">
            <div id="shareBtn" className="btn btn-success clearfix">Share Dialog</div>

            <p>The Share Dialog enables you to share links to a person's profile without them having to use Facebook
                Login. <a href="https://developers.facebook.com/docs/sharing/reference/share-dialog">Read our Share
                    Dialog
                    guide</a> to learn more about how it works.</p>
            <button onClick={fbstart}>Click</button>
        </div>
    );
}

export default App;

但是,我应该从哪里导入变量FB,因为我遇到了我的项目FB中没有的错误。
问题:如何在此功能中正确集成此功能反应申请?

1 个答案:

答案 0 :(得分:0)

您需要在程序中包含FB SDK

<script>
  window.fbAsyncInit = function() {
    FB.init({
      appId            : 'your-app-id',
      autoLogAppEvents : true,
      xfbml            : true,
      version          : 'v8.0'
    });
  };
</script>
<script async defer crossorigin="anonymous" src="https://connect.facebook.net/en_US/sdk.js"></script>

还要注意,您需要一个Faceboo AppId。如果您尚未制作应用,则可以访问developers.facebook.com并点击我的应用