Last active
August 8, 2021 03:54
-
-
Save S-codes14/01c95f7d3954e3fa4744057bd045817e to your computer and use it in GitHub Desktop.
react component, facebook hook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React from "react"; | |
| // Injects the Facebook SDK into the page | |
| const injectFbSDKScript = () => { | |
| (function (d, s, id) { | |
| var js, | |
| fjs = d.getElementsByTagName(s)[0]; | |
| if (d.getElementById(id)) { | |
| return; | |
| } | |
| js = d.createElement(s); | |
| js.id = id; | |
| js.src = "https://connect.facebook.net/en_US/sdk.js"; | |
| fjs.parentNode.insertBefore(js, fjs); | |
| })(document, "script", "facebook-jssdk"); | |
| }; | |
| export const useInitFbSDK = () => { | |
| const [isInitialized, setIsInitialized] = React.useState(false); | |
| // Initializes the SDK once the script has been loaded | |
| // https://developers.facebook.com/docs/javascript/quickstart/#loading | |
| window.fbAsyncInit = function () { | |
| window.FB.init({ | |
| // Find your App ID on https://developers.facebook.com/apps/ | |
| appId: "<YOUR_APP_ID>", | |
| cookie: true, | |
| xfbml: true, | |
| version: "v8.0", | |
| }); | |
| window.FB.AppEvents.logPageView(); | |
| setIsInitialized(true); | |
| }; | |
| injectFbSDKScript(); | |
| return isInitialized; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment