Abhishek Sharma
10/17/2024, 2:57 AM@Composable
private fun DisplayUserProfile() {
val userViewModel: UserProfileViewModel = rememberViewModel()
if (userViewModel.userProfileStatus.isSuccess()) {
ShowUserProfile { userProfileState ->
when (userProfileState) {
is UserProfileState.Loading -> userViewModel.onProfileLoading()
is UserProfileState.Loaded -> userViewModel.onProfileLoaded(userProfileState.data)
}
}
}
}
@Composable
private fun SomeScreenImpl(
modifier: Modifier
) {
HandleProfileUpdate(
onUpdate = {
userViewModel.reset()
userViewModel.fetchUserProfile()
},
onCancel = {
userViewModel.reset()
}
)
}
and VM has this
var userProfileStatus: ViewResult<UserProfile> by mutableStateOf(ViewResult.Uninitialized)
fun fetchUserProfile() {
viewModelScope.launch {
userProfileStatus = ViewResult.Loaded(UserProfile("John Doe", "john.doe@example.com"))
}
}
I am seeing ShowUserProfile
composable being recomposed twice, even though fetchUserProfile
is called only once. Not sure from where and how the second recomposition is happening. Any help would be really appreciated?Zach Klippenstein (he/him) [MOD]
10/17/2024, 3:46 PMreset
method do?
3. Is ShowUserProfile
being recomposed or is DisplayUserProfile
being recomposed and calling ShowUserProfile again? If the former, what’s that composable look like?