LeoColman
11/19/2021, 4:24 PM@NoLiveLiterals
@Composable
fun DonationForm() {
var selected by remember { mutableStateOf(values[1]) }
val coroutineScope = rememberCoroutineScope()
val clientSecret = coroutineScope.async { startDonation(selected.toLong().times(100)) }
Hey guys!
How to make clientSecret render again everytime selected is changed?
selected gets it's state correctly, but clientSecret is being calculated only once (and not recalculating on recomposition)LeoColman
11/19/2021, 4:26 PMval clientSecret = selected.run { coroutinescope.... }
worksLeoColman
11/19/2021, 4:26 PMBig Chungus
11/19/2021, 4:27 PMvar clientSecret by remember { mutableStateOf(1L) }
coroutineScope.launch {
clientSecret = startDonation(selected.toLong().times(100))
}Big Chungus
11/19/2021, 4:28 PMLeoColman
11/19/2021, 4:28 PMBig Chungus
11/19/2021, 4:29 PMBig Chungus
11/19/2021, 4:29 PM...)?LeoColman
11/19/2021, 4:30 PMselected.run I'd trigger recomposition because selected would change inside a @Compose functionLeoColman
11/19/2021, 4:30 PMBig Chungus
11/19/2021, 4:32 PMLeoColman
11/19/2021, 4:33 PMDonationFormLeoColman
11/19/2021, 4:33 PMselected lineLeoColman
11/19/2021, 4:33 PMBig Chungus
11/19/2021, 4:35 PMBig Chungus
11/19/2021, 4:35 PMBig Chungus
11/19/2021, 4:35 PMBig Chungus
11/19/2021, 4:36 PMhfhbd
11/19/2021, 5:13 PMderivedState? Or remember(selected)?LeoColman
11/19/2021, 5:54 PMLeoColman
11/19/2021, 5:54 PMhfhbd
11/19/2021, 6:38 PMderivedState updates the calculation if a state used inside the calculation block was changed.
alternative using selected as a key for remember will result into the same behavior: if the key of remember was changed, the calculation will be executed again. Difference between remember and derivedState is optimization, in derivedState the compiler knows, the reason for recalculation was this state change, so it the recalculation can be scheduled right after the change of the state (change, recalculation, recomposing), and not after changing, recomposing and recalculation and recomposing again.Zach Klippenstein (he/him) [MOD]
11/22/2021, 4:23 PM