Created
November 22, 2023 18:29
-
-
Save shahzadansari/aba0434ba421a6eeefc533aab21c50fc to your computer and use it in GitHub Desktop.
Sample showing how logs can cause recompositions and how to fix them
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable | |
fun LogsCausingRecompositions() { | |
Box(modifier = Modifier.fillMaxSize()) { | |
var count by remember { mutableIntStateOf(0) } | |
Log.d("MyTag:", "Count: $count") | |
Button( | |
onClick = { count++ }, | |
content = { Text("Increment") }, | |
modifier = Modifier.align(Alignment.Center) | |
) | |
} | |
} | |
@Composable | |
fun Fixed() { | |
Box(modifier = Modifier.fillMaxSize()) { | |
var count by remember { mutableIntStateOf(0) } | |
SideEffect { | |
Log.d("MyTag:", "Count: $count") | |
} | |
Button( | |
onClick = { count++ }, | |
content = { Text("Increment") }, | |
modifier = Modifier.align(Alignment.Center) | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fix using
SideEffect
fix-logs-recompositions.mov