Colton Idle
03/13/2022, 5:29 PM@Composable
fun SomeScreen(
viewModel: SomeScreenViewModel = hiltViewModel(),
someIdINeed: String = ""
) {
viewModel.init(someIdINeed)
MyActualScreenContent()
}
The viewModel.init(id) should go inside of a remember{}? Or is it better to do LaunchedEffect(Unit)?
edit: I almost wonder how viable it'd be to create a lint rule or something that would flag this sort of stuff in a composable. The above code was in our codebase for like 3 months before i noticed it just now.Adam Powell
03/13/2022, 5:43 PMAdam Powell
03/13/2022, 5:45 PMUnit
-returning non-@Composable
functions on a parameter object in a composable or something?Colton Idle
03/13/2022, 6:43 PMhfhbd
03/13/2022, 8:11 PMtheapache64
03/13/2022, 8:15 PMBundle
and then access it using a SavedStateHandle
in the ViewModel
? if you’re using navigation compose, this is already there.Colton Idle
03/13/2022, 8:30 PMtheapache64
03/13/2022, 8:34 PMlateinit var
inside viewModel to prevent multiple calls when navigated back.
fun start(){
if(isInitialized) return
// ...
}
what do you think ?Colton Idle
03/13/2022, 8:35 PMAdam Powell
03/13/2022, 9:17 PMremember {}
- keep in mind that until the composition is applied, nothing that executed in a composable has "happened" yet. Beware of executing side effects that leak this way.Adam Powell
03/13/2022, 9:17 PMAdam Powell
03/13/2022, 9:19 PMremember {}
from parameters without a key.Adam Powell
03/13/2022, 9:20 PMSideEffect
doesn't take keys - it expects whatever it does to implement idempotence somewhere elseColton Idle
03/13/2022, 9:30 PMColton Idle
03/13/2022, 9:30 PMnitrog42
03/14/2022, 1:12 PMidStateFlow.emit(someId)
, which won't reemit the same value so it's kinda ok for me.
I could also do something like
LaunchedEffect(viewModel, someId) {
viewModel.init(someId)
}
but like you i'm not sure what is the general consensus here (but like some others comments I try to do it through navArgs and getting them from the SavedStateHandle in the viewModel to avoid having it in UI code)