hello! same as <@U04E7GTQW72> above I ran into the...
# multiplatform
v
hello! same as @Christian Ricardo above I ran into the same issue, but my question is rather of a different nature, I'm trying to use the
org.jetbrains.androidx.lifecycle:lifecycle-viewmodel
. This worked with WASM but because of the issues mentioned above (you can't copy paste either) when I tried to run with JS and then it started crashing when trying to deploy I get this exception:
Copy code
IllegalStateException: No ViewModelStoreOwner was provided via LocalViewModelStoreOwner
I am aware that web doesn't support reflection so my view model is provided by something like this:
viewModel { MyViewModel() }
. I've been wracking my brain trying to figure out if KMP can offer a complete web experience or should I go with something else for this task alone? (Android and iOS work nicely) Thank you for you time!
I've tried a much simpler example:
Copy code
fun main() {
    renderComposable(rootElementId = "root") {
        // Provide explicit initializer: required on non-JVM targets :contentReference[oaicite:3]{index=3}
        val vm: CounterViewModel = viewModel { CounterViewModel() }

        val count = vm.count.collectAsState()

        Div {
            Button(
                attrs = {
                    onClick { vm.increment() }
                }
            ) {
                "Clicked ${count.value} times"
            }
        }
    }
}

class CounterViewModel : ViewModel() {
    private val _count = MutableStateFlow(0)
    val count: StateFlow<Int> = _count

    fun increment() {
        viewModelScope.launch {
            _count.value += 1
        }
    }
}
this results in the same exception
Copy code
IllegalStateException: No ViewModelStoreOwner was provided via LocalViewModelStoreOwner
a
@Oleksandr Karpovich [JB] ^^
v
I learnt that compose multiplatform doesn't work with web, not explicitly mentioned but that's what i got from this. It's not quite the seamless environment, would be nice to clearly document these things. Thank you anyway! 😄
o
Hey! I just noticed that you tried to use ViewModel with compose.html. compose.html is not a part of Compose Multiplatform. The ViewModel library is supposed to be used with Compose Multiplatform. Still, I think you can try to provide your own
LocalViewModelStoreOwner
:
Copy code
LocalViewModelStoreOwner.provides(...)
v
I tried providing: LocalViewModelStoreOwner then it asked to provide the local densities, but thanks for clarifying, i'm just trying to make sense of KMP CMP and Compose for HTML 🙂 appreciate all the help
o
then it asked to provide the local densities
got it 🙂 you probably trying the untravelled path, which might lead to something interesting and useful
🤞 1