importing static javascript functions into scala.js

Viewed 33

In a third party library react-native-fbsdk-next I have a file FBAccessToken.ts The class below has a few ordinary members and a static member function.

class FBAccessToken {

  accessToken: string;
  permissions: Array<string>;

  static getCurrentAccessToken() {
    return new Promise<FBAccessToken | null>((resolve) => {
    ...
    }
  }
}

export default FBAccessToken;

I need to import both the class members as well as the static function.

My strategy is to use a scala class and a companion object like so

  @native
  @JSImport("react-native-fbsdk-next", "FBAccessToken")
  class FBAccessToken extends js.Object {
    def accessToken: String = native
    def permissions: Array[String] = native
  }

  @native
  @JSImport("react-native-fbsdk-next", "FBAccessToken")
  object FBAccessToken extends js.Object {
    def getCurrentAccessToken: Promise[FBAccessToken | Null] = native
  }

But the static function to companion object import seems to not work. sbt fastOptJs works but on running the app in a simulator I get

TypeError: undefined is not an object (evaluating '_$$_REQUIRE(_dependencyMap[4], "react-native-fbsdk-next").FBAccessToken.getCurrentAccessToken')

What am I doing wrong?

1 Answers

FBAccessToken is exported under the default export in the JavaScript library:

export default FBAccessToken;

and not under its own name "FBAccessToken".

Therefore, when importing it from Scala.js, you have to use JSImport.Default as the second argument ot @JSImport:

@JSImport("react-native-fbsdk-next", JSImport.Default)

This applies both to the class and to the object

Related