https://kotlinlang.org logo
#compose
Title
# compose
s

Se7eN

01/08/2021, 11:04 AM
I'm using
SnapshotStateList<SnapshotStateList<MyObject>>
as a state but when I change it, the composables aren't getting recomposed. However, if I wrap it with a mutable state
MutableState<SnapshotStateList<SnapshotStateList<MyObject>>
it's working fine. Is this intended?
Copy code
data class MyStateHolder(
    val name: MutableState<String>,
    val list: SnapshotStateList<SnapshotStateList<MyObject>>
)

var state = remember { MyStateHolder(...) }
...
MyComposable(onSomeEvent = { newList -> state = state.copy(list = newList) })
SomeOtherComposable(state.list) // not getting recomposed when I change state with a new list
I'm not sure but I think it works fine if I just change an item of the list as opposed to copying the whole state for a new list. This works fine:
Copy code
data class MyStateHolder(
    val name: MutableState<String>,
    val list: MutableState<SnapshotStateList<SnapshotStateList<MyObject>>>
)

val state = remember { MyStateHolder(...) }
...
MyComposable(onSomeEvent = { newList -> state.list = newList })
SomeOtherComposable(state.list)
d

Dominaezzz

01/08/2021, 3:01 PM
Looking at your code, I don't think you need
SnapshotStateList
. Just use
List
and
String
,
Recomposition will only happen when a
State
object changes.
s

Se7eN

01/08/2021, 4:01 PM
I didn't include the whole code but I'd like to do
state.list[i][j] = something
too so I need the SnapshotStateList
d

Dominaezzz

01/08/2021, 4:13 PM
Oh, in that case just do the latter code snippet.
s

Se7eN

01/08/2021, 4:13 PM
Okay thanks!
3 Views