Thread
#compose
    Michal Bacik

    Michal Bacik

    2 years ago
    Please help - how can I make "local state" of widget that reflects to change and rebuilds the widget when changed? Here I want to change color of rectangle after click on it. But it's not rebuilt. I suppose I don't need global
    @Model
    class for this scenario.
    @Composable
    fun MyRect(){
        var color = +memo{ Color.Green }
        Clickable(onClick = {
            color = Color.Red
        }) {
            ColoredRect(brush = SolidColor(color), height = 100.dp)
        }
    }
    Luca Nicoletti

    Luca Nicoletti

    2 years ago
    val color = +state{ Color.Green }
    works
    you then need to unwrap the state with
    color.value
    when needed
    Grigorii Yurkov

    Grigorii Yurkov

    2 years ago
    var color by +state { Color.Green }
    Luca Nicoletti

    Luca Nicoletti

    2 years ago
    val*
    Michal Bacik

    Michal Bacik

    2 years ago
    Perfect, works. Thanks! I've seen
    memo
    somewhere, but that's probably for different thing.
    Grigorii Yurkov

    Grigorii Yurkov

    2 years ago
    if it will be val, how can you change it
    Luca Nicoletti

    Luca Nicoletti

    2 years ago
    You change
    color.value
    Michal Bacik

    Michal Bacik

    2 years ago
    it's
    val
    actually. you change its
    value
    .
    Luca Nicoletti

    Luca Nicoletti

    2 years ago
    not
    color
    itself
    Grigorii Yurkov

    Grigorii Yurkov

    2 years ago
    i wrote
    by
    , not
    =
    Michal Bacik

    Michal Bacik

    2 years ago
    Hmm. There are two variants.
    var color by +state{ Color.Green }
    looks nicer, it doesn't require the
    .value
    thing.
    Grigorii Yurkov

    Grigorii Yurkov

    2 years ago
    yeah, i've seen it in sources
    Luca Nicoletti

    Luca Nicoletti

    2 years ago
    Yeah, looks better, as they;re also planning to get rid of
    +
    Michal Bacik

    Michal Bacik

    2 years ago
    And who knows for what is
    memo
    ? 🤔
    Luca Nicoletti

    Luca Nicoletti

    2 years ago
    It’s an
    Effect
    l

    Leland Richardson [G]

    2 years ago
    both
    val color = +state { ... }
    and
    var color by +state { ... }
    will work.
    we may at some point lint against
    var
    in a composable function that is not introduced by a property delegate. not sure yet though
    i prefer the
    var color by ...
    version for a couple of reasons, but some people might consider that more magic, so up to you
    Luca Nicoletti

    Luca Nicoletti

    2 years ago
    It looks nicer indeed
    Fudge

    Fudge

    2 years ago
    I say to get rid of the other way
    Having multiple ways of doing the same thing is just confusing
    Michal Bacik

    Michal Bacik

    2 years ago
    Proper way is
    var ... by
    . You're interested in your variable, not the delegate that is behind providing its value.
    Fudge

    Fudge

    2 years ago
    and also wars about the right way of doing it are not nice
    l

    Leland Richardson [G]

    2 years ago
    it’s definitely something worth considering
    in general i agree with “multiple ways of doing the same thing is bad”
    but it’s hard to know where you draw the line here….
    val x = a
    val y = x.first
    val z = x.second
    vs
    val (y, z) = a
    should kotlin make the 1st way impossible?
    Luca Nicoletti

    Luca Nicoletti

    2 years ago
    Fudge

    Fudge

    2 years ago
    Hmm, I wrote a long message about why that's not the same thing, but that got deleted because of internet connectivity. Anyway, the conclusion is that at the very least there should be an inspection to use the
    by
    syntax and to only use the
    by
    syntax in official examples.
    Luca Nicoletti

    Luca Nicoletti

    2 years ago
    Why? What if I need a
    State
    object?
    Fudge

    Fudge

    2 years ago
    Why would you need a
    State
    object?
    Luca Nicoletti

    Luca Nicoletti

    2 years ago
    Maybe I want
    component1
    and
    component2
    Fudge

    Fudge

    2 years ago
    Maybe
    But why would you need a State object?
    Luca Nicoletti

    Luca Nicoletti

    2 years ago
    If you don’t have the state object you can’t get
    compoent1
    and
    component2
    of
    State
    Btw, I already said the
    by
    solution looks cleaner and I’d use that, but as @Leland Richardson [G] said it’s hard to know where to force something
    Fudge

    Fudge

    2 years ago
    oh, you mean
    (someState,setSomeState)
    Luca Nicoletti

    Luca Nicoletti

    2 years ago
    yes
    Fudge

    Fudge

    2 years ago
    Why would you use this syntax over using
    by
    ?
    Right now it seems to me like that should begone too
    Grigorii Yurkov

    Grigorii Yurkov

    2 years ago
    val state = +state { false }
    Checkbox(checked = state.value, onCheckedChange =    state.component2())
    var checked by state
    Fudge

    Fudge

    2 years ago
    ```
    That's... unreadable
    I don't think that's desired
    I would rather read
    var state by state { false }
    Checkbox(checked = state.value, onCheckedChange  = {state = it})
    But I could see
    val (state,setState) = state { false }
    Checkbox(checked = state.value, onCheckedChange =    setState)
    But is this tiny synctactic sugar really worth having 3 ways of doing the same thing?
    Luca Nicoletti

    Luca Nicoletti

    2 years ago
    Well that’s 2 ways of doing the same 😛
    Fudge

    Fudge

    2 years ago
    What do you mean? You can 1) do
    val (value,setValue)  = state { false }
    2)
    val value = state {false}
    3)
    var value by state { false }
    Luca Nicoletti

    Luca Nicoletti

    2 years ago
    Yeah, the second one is bundle into the first
    What I meant was:
    = +state{}
    could be used for
    1.
    ,
    by
    could not. Maybe that’s the reason for having both of them 🙂
    andrew

    andrew

    2 years ago
    the first one is a lot like react hooks
    Luca Nicoletti

    Luca Nicoletti

    2 years ago
    Well that’s because Compose is influenced by React as well, alongside by Flutter, Vue.js, Litho