Saiedmomen
04/27/2022, 7:35 PMSideEffect
not having key args?
In the example in the official docs, you can see that there easily can be extra calls to the analytics.
The LaunchedEffect
is great but the suspend block is sometimes not needed and wasteful.
@Composable
fun rememberAnalytics(user: User): FirebaseAnalytics {
val analytics: FirebaseAnalytics = remember {
/* ... */
}
// On every successful composition, update FirebaseAnalytics with
// the userType from the current User, ensuring that future analytics
// events have this metadata attached
SideEffect {
analytics.setUserProperty("userType", user.userType)
}
return analytics
}
tad
04/27/2022, 7:40 PMDisposableEffect(key) {
...
onDispose { }
}
SideEffect
with a key
doesn't make much sense to implement as its purpose is to run on every composition.Saiedmomen
04/27/2022, 7:42 PMonDispose
blocktad
04/27/2022, 7:42 PMSaiedmomen
04/27/2022, 7:43 PMtad
04/27/2022, 7:44 PM@Composable
inline fun OneShotEffect(key: Any, noinline block: () -> Unit) {
DisposableEffect(key) {
block()
onDispose { }
}
}
Saiedmomen
04/27/2022, 7:49 PMtad
04/27/2022, 7:50 PMSaiedmomen
04/27/2022, 7:51 PMtad
04/27/2022, 7:53 PMLaunchedEffect
schedules its block on the main dispatcher, so its changes will always be reflected at least one frame laterSaiedmomen
04/27/2022, 7:55 PMSideEffect
at all.
You almost never want an effect to repeat on each compositiontad
04/27/2022, 7:56 PMremembered
state object through function parametersSaiedmomen
04/27/2022, 7:58 PMremeber(arg) {}
?tad
04/27/2022, 7:59 PMarg
changesSaiedmomen
04/27/2022, 8:01 PMRick Regan
04/30/2022, 1:39 PMSaiedmomen
04/30/2022, 6:21 PM