dave08
01/03/2024, 10:53 AMnavController.navigate(...)
that usually stays the same for that composable...vide
01/03/2024, 12:21 PM.clickable
modifier updates the saved lambda on every recomposition if it has changed. LaunchedEffect
does not restart if its keys do not change.vide
01/03/2024, 12:22 PMAbstractClickableNode
and it's updateCommon
methoddave08
01/03/2024, 12:34 PMdave08
01/03/2024, 12:36 PMif
to decide what to do based on something outside of it, maybe I'd understand... but if not, then we still need to use rememberUpdatedState?vide
01/03/2024, 12:40 PMyschimke
01/03/2024, 12:41 PMvide
01/03/2024, 12:44 PMLaunchedEffect
lambda holds a reference to the old value in its closure even if it changesvide
01/03/2024, 12:46 PM@Composable fun Foo(activate: () -> Unit) {
LaunchedEffect(Unit) {
delay(5000)
activate()
}
}
vs
@Composable fun Foo(activate: () -> Unit) {
val updatedActivate = rememberUpdatedState(activate)
LaunchedEffect(Unit) {
delay(5000)
updatedActivate.value()
}
}
vide
01/03/2024, 12:47 PMFoo
cannot know in advance if it will be ever used by a parent that will recompose it with a different activate
before it calls activate()
dave08
01/03/2024, 12:48 PMdave08
01/03/2024, 12:49 PM@Composable fun Bar(nav: NavController) { Foo { nav.navigate("some-destination") } }
would never change... what would be considered that could change?dave08
01/03/2024, 12:50 PM@Composable fun Bar(nav: NavController, dest: String) { Foo { nav.navigate(dest) } }
?dave08
01/03/2024, 12:51 PMvide
01/03/2024, 12:52 PMvide
01/03/2024, 12:52 PMdave08
01/03/2024, 12:52 PMWhen LaunchedEffect enters the composition it will launch block into the composition's CoroutineContext. The coroutine will be cancelled and re-launched when LaunchedEffect is recomposed with a different key1. The coroutine will be cancelled when the LaunchedEffect leaves the composition.
dave08
01/03/2024, 12:53 PMvide
01/03/2024, 12:53 PM@Composable fun Qux(isLoggedIn: Boolean) {
Bar(navController, if (isLoggedIn) "target1" else "target2")
}
vide
01/03/2024, 12:55 PMvide
01/03/2024, 12:55 PMvide
01/03/2024, 12:56 PMvide
01/03/2024, 12:56 PMLaunchedEffect(activate) { ... }
dave08
01/03/2024, 12:56 PMvide
01/03/2024, 12:56 PMvide
01/03/2024, 12:58 PMrememberUpdatedState
with LaunchedEffect when you have a long running operation in LaunchedEffect that you also don't want to restart for some reasondave08
01/03/2024, 1:03 PMyou also don't want to restart for some reasonBy using `LaunchedEffect`'s parameter to restart on that lambda change instead? But otherwise, it would only get cancelled when
Foo
doesn't get called on a future recomposition and the task is still running?vide
01/03/2024, 1:07 PMvide
01/03/2024, 1:08 PMdave08
01/03/2024, 1:09 PMvide
01/03/2024, 1:27 PMvide
01/03/2024, 1:27 PM