i might be misunderstanding how `rememberSaveable...
# compose-desktop
n
i might be misunderstanding how
rememberSaveable
works but i have a few tabs in my app set up with a
when (index) 0 -> tabOne() 1 -> tabTwo() }
to switch the content in the content of these tabs i use
var text by rememberSaveable(key = "textfield") { mutableStateOf(TextFieldValue("")) }
but every time the tab changes and come back the value resets to empty? does this get wiped every time it does not get rendered ? how would i solve this in a way that does not involve storing my own global state (mutablestateflow) for each thing ?
m
This is expected because when you move to a new tab you're disposing the current tab from Composition (based on the way you do it right now) and so any state attached with it will be cleared
e
Even when using
rememberSaveable
?
m
@eygraber Yes, AFAIK.
rememberSaveable
helps you in saving things like scrolling position on desktop and of course surviving config changes on Android but basically when you do something like this:
Copy code
when(something) {
   1 -> show some composable
   2 -> show another composable
}
the two conditions are mutually exclusive so they won't be in composition at the same time.
if you want to use
rememberSaveable
here you'll need to hoist it somewhere up the tree
n
so when i switch from 1 to 2 it discards ALL state in branch 1 ?
m
@Nikky yep exactly
n
quite annoying since this is a deepdly nested UI with lists of different lengths
this is what we are doing

https://nikky.catgirl.host/i/cgau2vw8.pngâ–¾

and i think the "correct" solution here is to store it all in some stateflow and write / read then out of a map by key
using a global
Copy code
private val expandedStates = mutableStateMapOf<String, Boolean>()
fixed the problem.. and now i also understand why, thanks
đŸ‘Œ 1
s
n
so i guess i could pass some MapString, Boolean through that Saveable state holder ? but i don't actually need it to be saveable.. i just want it to not loose its state when the composition gets discared as you navigate around.. a global to store these flags works well enough and does not complicate the code as much good to know it exists though.. i am sure there is usecases where i will remember and use this
a
That’s exactly what SaveableStateHolder does