Miguel Coleto

    Miguel Coleto

    2 years ago
    Do we have to place
    mutableStateOf
    always inside of
    remember
    ? It is not clear to me what is
    remember
    doing 🤔
    5 and 6
    Miguel Coleto

    Miguel Coleto

    2 years ago
    After reading this I'm still a little bit confused. I'll read it again more slowly
    But do you know what is the equivalent for this construct in React?
    Coming from 2017's React and little knowledge of React Hooks my understanding is that you would only need mutableStateOf
    shikasd

    shikasd

    2 years ago
    The important part here is that whole composable function is kinda like render in react, so it is re-evaluated every time some value has changed. That's why you would want remember, it will keep the value between those re-evaluations, so your state won't be lost.
    You can imagine it as keeping the state outside of function (like a field in react component class), but you don't have to manage the instance of this component, you are just remembering things
    Javier

    Javier

    2 years ago
    I have no idea about react, sorry
    Miguel Coleto

    Miguel Coleto

    2 years ago
    So mutableStateOf should always be inside remember?
    ppvi

    ppvi

    2 years ago
    if you don't put it inside of a remember, you would be creating the MutableState on every composition
    Miguel Coleto

    Miguel Coleto

    2 years ago
    Probably this has to do with the fact that Composable functions return Unit instead of being pure like in React right?
    ppvi

    ppvi

    2 years ago
    how so?
    shikasd

    shikasd

    2 years ago
    Being pure is the whole another topic I think. Hooks in react (useEffect and useState) are much closer to what remember is and they have quite similar reasoning behind them as well.
    Miguel Coleto

    Miguel Coleto

    2 years ago
    If composable functions were part of a class and they would return immutable copies of that class, you don't need to remember the state because you already have it as a field in the class
    ppvi

    ppvi

    2 years ago
    so... you're saying they're pure, which seems to contradict your question
    Miguel Coleto

    Miguel Coleto

    2 years ago
    I'm saying if they were
    ppvi

    ppvi

    2 years ago
    Miguel Coleto

    Miguel Coleto

    2 years ago
    I think that I have some conflicts with this mental model coming from pure functional programming but I get it
    Thanks for the resources guys 👍
    l

    Leland Richardson [G]

    2 years ago
    I talk about remember (and the fact that composable functions have a “memory”, which is what remember addresses) in my talk in a few places:

    here

    and

    here

    the equivalent in react is
    useMemo
    fwiw
    useState is equivalent to the
    state
    function we used to have, which was simply a convenience method:
    @composable fun <T> state(init: () -> T) = remember { mutableStateOf(init()) }
    if you are creating a state object inside of composition, you likely want to
    remember
    it or else when a recomposition happens it will just create a new one instead of using the old one (which is the whole idea behind remember… it “remembers” the previous value).
    The neat thing about
    mutableStateOf
    is that you don’t need to call it in a composable function. If you are creating state somewhere else (say, in the construction of a class), you can create a state object there as well. This creates use cases that are similar to the new React library recoil if you are familiar with that (and also similar to mobx if you are familiar with that)
    I also go into a lot of detail about how remember actually works in this talk

    here

    If you are familiar with recoil, here is a gist of recoil’s docs examples equivalent in compose: https://gist.github.com/lelandrichardson/4cfbb7aab96e01803cc7b289c2303e45
    Miguel Coleto

    Miguel Coleto

    2 years ago
    Amazing, thank you 👍
    Karthick

    Karthick

    2 years ago
    Why state does not remember itself other than remember { state }, we don't wrap state in memo in react 🤔
    l

    Leland Richardson [G]

    2 years ago
    because in react, state is tied to the component itself
    in compose it’s just an object
    you can call
    mutableStateOf
    anywhere
    Karthick

    Karthick

    2 years ago
    So whenever we use the values of state object, composable functions will subscribe to it and whenever values changes it will recompose. Thank you