79577354

Date: 2025-04-16 14:07:54
Score: 0.5
Natty:
Report link

This issue happens because non-JVM platforms like wasmJs cannot use type reflection to instantiate ViewModels via the viewModel() function without an explicit initializer.

✅ Fix

Instead of relying on reflection, explicitly create your ViewModel and pass it into your Composable function manually.

✅ Working fix:

fun main {
ComposeViewport(viewportContainerId = "composeApplication") {

    val viewModel: MainViewModel = MainViewModel() // ✅ Create instance manually

    App(viewModel) // ✅ Pass it in

}
}

📚 Source

JetBrains Docs:

https://www.jetbrains.com/help/kotlin-multiplatform-dev/compose-viewmodel.html#using-viewmodel-in-common-code:

\>"On non-JVM platforms, objects cannot be instantiated using type reflection. So in common > code you cannot call the viewModel() function without parameters."

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Oto Oto