sonder-joker

    sonder-joker

    1 year ago
    I am a little confused about remember function . Compare to react usememory, what's the difference?
    k

    Kirill Grouchnikov

    1 year ago
    The very first Google result for "Compose vs React" gives this - https://tigeroakes.com/posts/react-to-compose-dictionary/#usememo--remember
    c

    CLOVIS

    1 year ago
    That page is not really helpful, because of how different state management works in Compose & React
    Remember is very similar to useMemo, but useState doesn't correspond to anything
    a

    Arkady Bazhanov

    1 year ago
    I thought
    useState
    is equivalent to
    remember { mutableStateOf(...) }
    c

    CLOVIS

    1 year ago
    I mean, yes and no. In its simpler form it is, but this works:
    var a by mutableStateOf(false)
    @Composable
    fun Foo() {
      Text(a)
      Button(... { a = !a })
    }
    and this doesn't:
    var a by useState(false)
    // “hooks cannot be used outside of functional components”
    val Foo = fc {
      //...
    }
    Essentially Compose understands state modification when you call State's setter, which can happen outside of a Composable (eg. global state) without any use of remember, but React only understands state modification when the
    useState
    setter is called, which can only happen locally in a component
    Basically,
    remember + mutableStateOf
    is much, much more powerful than
    useState
    So if you learned React and you want to learn Compose, yes it is the equivalent, but if you come from Compose and try to learn React (like I did) it doesn't help you at all
    Also, remember etc don't have anything like the rules of hooks https://reactjs.org/docs/hooks-rules.html