Are there cases where it’s better to consume a value as compose.runtime.State? #1865
-
In our app, we noticed unnecessary recompositions when using TextField components. Specifically, passing the value as a plain TextFieldValue caused recompositions in unrelated parts of the UI - I think that's because Compose determines whether to recompose based on equality checks. I have edited the tutorial example to demonstrate this: @Composable
fun Inbox(state: InboxScreen.State, modifier: Modifier = Modifier) {
Scaffold(
modifier = modifier,
topBar = { TopAppBar(title = { Text("Inbox") }) }) { innerPadding ->
LazyColumn(modifier = Modifier.padding(innerPadding).fillMaxSize()) {
item {
EmailSearch(state.searchQuery) { query ->
state.eventSink(InboxScreen.Event.SearchQueryChanged(query))
}
}
items(state.emails) { email ->
EmailItem(
email = email,
onClick = { state.eventSink(InboxScreen.Event.EmailClicked(email.id)) },
)
}
}
}
}
@Composable
private fun EmailSearch(query: State<String>, onQueryChanged: (String) -> Unit) {
TextField(
modifier = Modifier.fillMaxWidth().height(48.dp),
value = query.value,
onValueChange = onQueryChanged,
label = { Text("Search") },
)
}
Is there something I'm missing by understand how Compose update the UI?, or other benefits to consume it as the value itself. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Circuit state only has to be Stable, you can use stable properties in circuit state classes if you want. That said, the same standard compose design guidelines kick in here. Use callbacks, avoid passing state directly, etc. If you've measured this and found a meaningful performance issue, you could explore writing your state as an interface backed by state objects for this specific use case. But again - *if you've measured this. |
Beta Was this translation helpful? Give feedback.
Circuit state only has to be Stable, you can use stable properties in circuit state classes if you want. That said, the same standard compose design guidelines kick in here. Use callbacks, avoid passing state directly, etc. If you've measured this and found a meaningful performance issue, you could explore writing your state as an interface backed by state objects for this specific use case. But again - *if you've measured this.