Hey I have a quick question how something bothering me for some time assuming : ```val pagerState = ...
n
Hey I have a quick question how something bothering me for some time assuming :
Copy code
val pagerState = rememberPagerState(pageCount = 2)
can we write
Copy code
val selectedTabIndex = pagerState.currentPage
TabRow(selectedTabIndex = selectedTabIndex,...)
or do we have to do :
Copy code
val selectedTabIndex = remember(pagerState) { pagerState.currentPage }
?
which can be summed up by "can we still use standard variable declaration in a composable method or do we always have to use remember" ?
a
The former.
can we still use standard variable declaration in a composable method or do we always have to use remember
You can.
āž• 1
šŸ‘ 1
c
Basically: • If you use a normal variable, it will be re-initialized on every recomposition (= don't use normal variables for expensive calculations) • remember tells compose to observe that object and recompose if it changes (= for stuff that doesn't change, or shouldn't update the UI, remember is useless) For example, if you have a list of tabs, or whatever else that's hard-coded, won't change, and is cheap to compute, normal variables are fine
n
for the 2nd fact, is it remember or more the user of a mutableState ?
a
Remember and snapshot observation are orthogonal. Remember has no direct relation to snapshot observation, which is why you often see remember and mutableStateOf together; you often want to have both and need to say them both in those cases.
What remember will do is recompute the result value if one of its key parameters is different during a subsequent recomposition
z
If you want a longer explanation about remember vs mutableStateOf, this might help
šŸ‘ 2
n
well I like the first part of your serie, because what I find hard right now is to understand what really happens on recomposition (which code I wrote will be called again, which code won't)