Hello, I want to provide a different instance of a...
# koin
c
Hello, I want to provide a different instance of a ViewModel into many Composables.
Copy code
// Module
viewModel { MyViewModel() }
Copy code
class MyViewModel : ViewModel() { 
   var feedId: Int by Delegates.notNull()
}
Copy code
@Composable
fun MyComposable(myViewModel: MyViewModel = getViewModel(), feedId: Int) { // Using myViewModel with a specific feedId }
Copy code
feeds.forEach {
    MyComposable(feedId = it.feedId)
}
So each
MyComposable
can have its own viewModel, but what seems to happen to me is that all
MyComposable
are sharing the same instance of
MyViewModel
. Like
getViewModel()
is always providing the same instance. Am I right? Is there a way to provide different instances for each
MyComposable
?
👀 1
m
maybe you should use
factory
then?
Copy code
// Module
factory { MyViewModel() }