0

I have ReactNative project and I'm trying to call simple JS function from Kotlin (calling Kotlin from JS works fine). My project directory looks like this:
enter image description here

"shared" module is compiled as commonjs module.

In Main.kt I have (shortened):

external fun myFun()

In App.js I have (shortened):

import * as shared from './shared/build/classes/kotlin/main/shared.js'
export function myFun(){
    console.log("Hello")
} 

So I'm trying to call myFun() from Kotlin to get console log in JS. But I get exception:

myFun is not defined

I was looking to this docs:
https://kotlinlang.org/docs/reference/js-interop.html
https://kotlinlang.org/docs/reference/js-modules.html#applying-jsmodule-to-packages
But I not sure how to use those annotations (if they are needed?). Thanks for help.

1 Answer 1

1

I think you should annotate your kotlin declaration with

@JsModule("<module_name>") // seems like it is App
external fun myFun()

and setup module kind to umd in gradle sript like this

compileKotlin2Js.kotlinOptions.moduleKind = "umd"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.