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

Orhan Tozan

09/05/2020, 5:38 PM
Why is the mutableStateOf not wrapped by remember { } automatically? Having to write remember everywhere is just ugly and creates alot of noise
t

Timo Drick

09/05/2020, 5:40 PM
You could just create you own function to do that 😄
☝🏼 1
o

Orhan Tozan

09/05/2020, 5:41 PM
I know, but why isn't there in the first place?😁
t

Timo Drick

09/05/2020, 5:42 PM
Before it was stateOf and now they removed it to avoid confusion about the function behind this stuff. (I think it is just to make sure every one needs to think about it 😄 )
o

Orhan Tozan

09/05/2020, 5:42 PM
Soon or later, everybody will create that function, which would be just boilerplate. Seems easier if it was included in the library from the start.
Is there ever a case where one wouldn't use remember if he she is using mutableStateOf?
t

Timo Drick

09/05/2020, 5:44 PM
Yes when you want to use it inside of a normal class (not a composable function)
👆 1
o

Orhan Tozan

09/05/2020, 5:45 PM
But isn't it the point that State is used inside composable functions only? Classes use Livedata/flow
r

romainguy

09/05/2020, 5:46 PM
You don’t have to use live data or flow
t

Timo Drick

09/05/2020, 5:46 PM
Livedata is not so kotlin friendly and flow often a little bit over engineered. Also livedata is in my opinion more useful for the legacy view system.
r

romainguy

09/05/2020, 5:47 PM
Your own state classes makes sense too for intrinsic properties of a component
2
(Scroll state or whatnot)
a

Adam Powell

09/05/2020, 6:03 PM
Once you get past the basic examples the ratio of
remember { mutableStateOf(...) }
to
remember { MyStateClass() }
that uses
by mutableStateOf
internally to create separate observable properties becomes weighted rather heavily towards the latter
The presence of the old
state {}
helper for this created a hurdle that kept people juggling many state fields themselves long past when they should be grouped within an object, because they weren't comfortable with the component primitives involved
👆 1
It also caused people to think you could only create snapshot state objects during composition, and we observed people creating duplicate sources of truth and syncing between them to bridge the divide
👆 1
So we decided it was better not to have it to encourage better understanding in the 1.0 timeframe. Once these ideas and patterns become well understood in the wider community we might add it back for convenience
👌 1
r

Ray Ryan

09/05/2020, 7:43 PM
Convenience is a four letter word
It’s amazing how quickly a convenience becomes an obfuscation.
6
b

bohregard

09/08/2020, 9:18 PM
@Adam Powell can you elaborate a little bit on what you mean by
remember { MyStateClass() }
?
a

Adam Powell

09/08/2020, 9:33 PM
Objects like
Copy code
@Stable class MyStateClass(
    foo: Foo,
    bar: Bar
) {
    var foo by mutableStateOf(foo)
    var bar by mutableStateOf(bar)
}
that provide semantic grouping of several observable properties into a single concept
b

bohregard

09/08/2020, 9:35 PM
I see, is the annotation @stable required for remember also I assume that any property that changes will result in recomposition?
a

Adam Powell

09/08/2020, 9:37 PM
@Stable
is not required for remember, it's a signal that if you pass the same instance to a composable function that we can skip the child, since all of the object's data is observable and will notify the child if it changes
b

bohregard

09/08/2020, 9:37 PM
very neat
3 Views