https://kotlinlang.org logo
l

LeoColman

11/19/2021, 4:24 PM
Copy code
@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)
Doing
Copy code
val clientSecret = selected.run { coroutinescope.... }
works
I'm wondering if there's a way to do this differently
b

Big Chungus

11/19/2021, 4:27 PM
Hmm, try this?
Copy code
var clientSecret by remember { mutableStateOf(1L) }
coroutineScope.launch {
  clientSecret = startDonation(selected.toLong().times(100))
}
I have no idea why selected.run works 🤦
K 1
l

LeoColman

11/19/2021, 4:28 PM
lol
b

Big Chungus

11/19/2021, 4:29 PM
I'd assume selected.run compiles to the same thing as your first case (run being an inline fun)
Maybe you're doing something different in the ommitted part (
...
)?
l

LeoColman

11/19/2021, 4:30 PM
I'll try again. My assumption was tthat with
selected.run
I'd trigger recomposition because
selected
would change inside a @Compose function
Not inside the coroutine async block, if that makes any sense
b

Big Chungus

11/19/2021, 4:32 PM
But run is not "Composable
l

LeoColman

11/19/2021, 4:33 PM
It isn't, but i'm inside
DonationForm
My thought was that triggering DonationForm recomposition would trigger the
selected
line
The run was used only to get the value in an expression, which was what I thought could trigger the recomposition to work
b

Big Chungus

11/19/2021, 4:35 PM
Then I think my proposed solution is less hacky
Give it a go
You could also try using SideEffects API for this as well.
☝🏻 1
That way you can control when the effects are executed better (as opposed to launching coroutine each composition)
h

hfhbd

11/19/2021, 5:13 PM
what about
derivedState
? Or
remember(selected)
?
l

LeoColman

11/19/2021, 5:54 PM
Your method works, @Big Chungus
I don't know what either of those are @hfhbd 😂
h

hfhbd

11/19/2021, 6:38 PM
derivedState
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.
z

Zach Klippenstein (he/him) [MOD]

11/22/2021, 4:23 PM
In general you don't want to perform side effects like launching async work directly from compositions. Use effect handlers.
👍 2
6 Views