using `Map` like: `mutableStateOf(map)` and reassi...
# compose
m
using
Map
like:
mutableStateOf(map)
and reassigning that
Map
doesn’t trigger recomposition? how can I approach this?
t
what kind of things do you store in the map?
m
Map<enum class, Double>
j
m
thanks! I’ll take a look. didn’t know 6th part came out 🦜
t
yea that’s really nice article, maybe you could try to figure it out with a flow
j
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
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
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
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
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
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
786 Views