How to import Scalajs Facade with react-native? Error: $g.algoliasearch is not a function

198 views Asked by At

I want to use facade Algoliasearch in my scalajs app, I imported it through sbt and but on UI I see $g.algoliasearch is not a function. I assume this is because of missing javascript libraries. I have included Algoliaseach-client-javascript dependencies in my package.json and yarn installed it.

Now I'm not sure when I use facade how I provide/link those dependencies. If someone could provide some example code that would be helpful

I am talking about any one of these facades https://github.com/DefinitelyScala/scala-js-algoliasearch

I have added jsDependencies like this

jsDependencies ++= Seq( "org.webjars.bower" % "github-com-algolia-algoliasearch-client-javascript" % "3.18.1" / "3.18.1/reactnative.js" ) I was hoping that above will declare something like this in my scalajs.output.ios file var Algoliasearch = require(...) but I do not see any entry like thisand I'm still not sure how to import it to avoid following error:

$g.algoliasearch is not a function

Any help what I'm missing here please?

1

There are 1 answers

2
Justin du Coeur On

There are two options:

1) Simply include the JavaScript dependencies as separate <script> tags, as normal for JavaScript and HTML. Note that these must come before the <script> tag for the Scala.js application.

2) Use the jsDependencies mechanism to build a -jsDeps.js file, which collects all of your dependencies into one file, and include that with a <script> tag. (Again, before the application itself.)

Note that the facade library can provide the jsDependencies, and your application can use that, but I don't personally recommend that -- it can lead to versioning conflicts between dependencies. It tends to work best if your application simply loads the JavaScript itself.

If you are sure that your HTML file is already including the JavaScript, and you're getting this error, check to make sure that the JavaScript is being loaded before the Scala.js application runs.