Skip to content

Instantly share code, notes, and snippets.

@S-codes14
Last active August 8, 2021 03:54
Show Gist options
  • Save S-codes14/01c95f7d3954e3fa4744057bd045817e to your computer and use it in GitHub Desktop.
Save S-codes14/01c95f7d3954e3fa4744057bd045817e to your computer and use it in GitHub Desktop.
react component, facebook hook
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