I’m trying to create a Facebook Instant HTML5 application in React.
As per their Quick Start documentation, they want me to install their SDK using a script tag, like so:
<script src="https://connect.facebook.net/en_US/fbinstant.6.3.js"></script>
I’ve created my app using create-react-app
. I’ve placed that snippet inside /public/index.html
so it looks like:
...
<body>
<noscript>You need to enable javascript to run this app.</noscript>
<script src="https://connect.facebook.net/en_US/fbinstant.6.3.js"></script>
...
They also provide the following snippet:
// Once all assets are loaded, tells the SDK
// to end loading view and start the game
FBInstant.startGameAsync()
.then(function() {
// Retrieving context and player information can only be done
// once startGameAsync() resolves
var contextId = FBInstant.context.getID();
var contextType = FBInstant.context.getType();
var playerName = FBInstant.player.getName();
var playerPic = FBInstant.player.getPhoto();
var playerId = FBInstant.player.getID();
// Once startGameAsync() resolves it also means the loading view has
// been removed and the user can see the game viewport
game.start();
});
Which I’ve placed in src/index.tsx'
.
This then gives me errors, saying:
'FBInstant' is not defined no-undef
Which likely means that the library is not being installed properly / brought into the proper namespace so that my React code can access it.
How can I get around this? Facebook tells me not to download and include it myself – they’ll reject my app.
Important notes:
Do not download and add the SDK to your bundle as it will be rejected in later steps.
This is a new way to build games on Facebook and does not support the Graph API.
So it seems I must use these <script>
tags. How can I make React recognise it?
2
Answers
Once you have added the
fbinstant
script tag in theindex.html
In your
src/index.tsx
, Add this to the top (before your snippet):As you explained, you’re getting this error because the typescript compiler does not recognize
FBInstant
, Since it is not installed.However in this case seems all you need is to make the compiler ignore these warning. The most suitable in my opinion is export it to a seperate
js
file (and notts
/tsx
), since it is a javascript code. Than import it as any node module.If you don’t like this option, alternative methods can be:
index.html
, right after yourscript
tag@ts-ignore
to suppress typescript errorsFBInstant
before the SDK script (more about this here)