I asked this in <#C01D6HTPATV|compose-desktop> yes...
# compose
l
I asked this in #compose-desktop yesterday but didn't get an answer. I think because it wasn't specifically a compose desktop question, but rather a compose question in general and better asked here. Detail are in the thread.
Copy code
class UserFormViewModel {
    val userRepository = UserRepository()
    var userData: UserData? by mutableStateOf(null)
}

@Composable
fun userForm(
    modifier: Modifier = Modifier,
) {
    val vm = remember { UserFormViewModel() }
    onActive {
        MainScope().launch {
            vm.userData = vm.userRepository.createNew()
        }
    }
    Column(
        modifier = modifier.padding(16.dp)
            .border(BorderStroke(2.dp, Color.LightGray)),
    ) {
        formRow(above = { Text("First Name: ") }) {
            TextField(
                value = vm.userData?.firstName ?: "",
                onValueChange = { vm.userData = vm.userData?.copy(firstName = it) }
            )
        }
        formRow(above = { Text("Last Name: ") }) {
            TextField(
                value = vm.userData?.lastName ?: "",
                onValueChange = { vm.userData = vm.userData?.copy(lastName = it) }
            )
        }
        formRow {
            Button(
                onClick = {
                    MainScope().launch {
                        vm.userData?.let { vm.userRepository.update(it) }
                    }
                }
            ) {
                Text("Save")
            }
        }
    }
}
This implementation recomposes all fields that rely on 
userData
 .  I would prefer this to not happen. I know I can break it out into individual fields in the 
UserFormViewModel
  class but then would need to manually create the userData class for repository use. Is there a method to use a data class without having to replace the object on input like the above?
d
What's wrong with recomposition?
l
TL;DR Possible performance issues due to re-rendering unnecessary ui elements in a more complex setting. I recently troubleshot a performance issue in a Vue application where when typing into a text field it became choppy because the updates were causing a re-render of all the field components. Granted that's a totally different platform/language/etc. The principal though of what needs to be rendered applies. Perhaps it's not applicable or possibly an indication of poorly structured entities. That's the back story.
I have 4 days of experience with compose in a playground project, so I'm very open to correction
d
I would say it's the responsibility of the library/plugin to deal with performance in this case. But that's just my opinion.
l
I agree with you to a degree. I do believe it is the developers responsibility to understand and use a library/tool within its given limitations. Given that I have received very little feedback on this over the past two days and I've seen some examples from somewhat established libraries that use data classes in this same way, I think it's probably safe to assume it not an issue that the authors or community has felt they needed to solve and therefore I don't either. Knowing how it works is sufficient for me to use my judgment for now. Thank you for taking the time to give your thoughts and input.
d
Just a small note. The team is currently throwing together a beta and a lot of this is still being designed. They'll probably get to this at some point but just not for beta.
👍 1
a
late answer, but I noticed this browsing the slack backlog. some general background that would be useful if you haven't seen it is https://developer.android.com/jetpack/compose/mental-model#recomposition
Compose's recompose-related optimizations are mostly built around skipping calls to other functions and reusing their old result provided their parameters and observed inputs haven't changed
so you can minimize amount of recomposition by splitting off heavy UI elements into other composable functions and passing them immutable parameters that rarely change
you don't necessarily need to "break it out into individual fields in the 
UserFormViewModel
  class". it should also work to pull out a copy of individual fields in a calling composable and pass them as a parameter to a callee