I am a little confused about remember function . C...
# compose-web
s
I am a little confused about remember function . Compare to react usememory, what's the difference?
k
The very first Google result for "Compose vs React" gives this - https://tigeroakes.com/posts/react-to-compose-dictionary/#usememo--remember
c
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
I thought
useState
is equivalent to
remember { mutableStateOf(...) }
c
I mean, yes and no. In its simpler form it is, but this works:
var a by mutableStateOf(false)
Copy code
@Composable
fun Foo() {
  Text(a)
  Button(... { a = !a })
}
and this doesn't:
Copy code
var a by useState(false)
// “hooks cannot be used outside of functional components”
Copy code
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