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

mattinger

10/01/2020, 12:57 PM
So, i’ve been building my own theme, and i’ve been wondering why Colors is treating the individual colors as mutable state, but typography doesn’t? I imagine maybe it’s helpful for the recomposer so that it doesn’t have to rebuild the full component tree if colors change, it can just adjust the color of the individual elements, but i’d like to confirm that.
Copy code
var primary by mutableStateOf(primary, structuralEqualityPolicy())
        internal set
l

Louis Pullen-Freilich [G]

10/01/2020, 1:23 PM
I imagine maybe it’s helpful for the recomposer so that it doesn’t have to rebuild the full component tree if colors change, it can just adjust the color of the individual elements, but i’d like to confirm that.
Exactly, it's very common for colors to change when switching between themes, or when animating one color in the theme (such as
primary
), so by having each color backed with
mutableStateOf
only components that read that particular color will be recomposed. On the other hand
Typography
is much more static, and barely ever changes - so there's not much point in having the same optimization there. Same for
Shapes
.
👍 1
2 Views