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

Marko Novakovic

11/26/2021, 8:25 PM
using
Map
like:
mutableStateOf(map)
and reassigning that
Map
doesn’t trigger recomposition? how can I approach this?
t

Teodor Penkov

11/26/2021, 8:26 PM
what kind of things do you store in the map?
m

Marko Novakovic

11/26/2021, 8:26 PM
Map<enum class, Double>
j

John O'Reilly

11/26/2021, 8:27 PM
m

Marko Novakovic

11/26/2021, 8:28 PM
thanks! I’ll take a look. didn’t know 6th part came out 🦜
t

Teodor Penkov

11/26/2021, 8:28 PM
yea that’s really nice article, maybe you could try to figure it out with a flow
j

John O'Reilly

11/26/2021, 8:28 PM
but I might be misunderstanding....when you say "reassigning that `Map`" do you mean updating values in the map or setting a new value to your
mutableStateOf
value?
m

Marko Novakovic

11/26/2021, 8:30 PM
neither works
article even says:
Copy code
If someone sent you a link to this post as a reply to a question on Slack or somewhere, and you just want a quick answer, here you go:

Don't put mutable collections inside mutable state holders.
hahahaha
😂 4
😀 3
e

eygraber

11/26/2021, 9:01 PM
I would think that the following should work:
Copy code
var map by mutableStateOf(mutableMapOf(1 to 2))
map = mutableMapOf(2 to 1)
That doesn't cause recomposition?
m

Marko Novakovic

11/27/2021, 9:55 AM
Copy code
private val _map = mutableMapOf()
val map by mutableStateOf(map)
.....
_map[key] = value
map = HashMap(_map)
the gist of what am doing it does’t trigger recomposition
I tried just assigning it without
HashMap
just
map = _map
but that doesn’t work either I introduced
HashMap
in hope that creating new object will work
p

philglass

11/28/2021, 9:50 PM
You're doing exactly what the article above tells you not to, nested mutability with a
MutableState<MutableMap<X, Y>>
. The value you're assigning to
map
is equal to its current value (in this case "deep equal", not reference equal, but the effect is the same), so by design no change notifications are sent.
m

Marko Novakovic

11/29/2021, 7:46 AM
today I’ll read the article, finally
one thing tho, I used
SideEffect
and it actually triggers on every change so I added
remember(state.map) { mutableStateOf(state.map) }
and everything works
491 Views