Shakil Karim
12/25/2020, 11:00 AMvar imageBitmap by remember { mutableStateOf<ImageBitmap?>(null) }
SwiftUI syntx read so much better
@State var someValue = ""pavi2410
12/25/2020, 4:37 PMstate
function/delegate. It's deprecated nowpavi2410
12/25/2020, 4:37 PMAdam Powell
12/25/2020, 10:39 PMvar someValue by state { "" }
led to a lot of confusion for developers in terms of how and where observable state holders and property delegates can be created, so we encourage the longer version as the preferred approach. Decomposing it into the distinct mechanisms of remember
to create persistence in composition and mutableStateOf
to create an observable state holder led to better understanding of how things work and what's possible.Adam Powell
12/25/2020, 10:42 PMvar someValue by remember { mutableStateOf("") }
often and repeated enough to where it gets onerous or noisy to read, it's often a sign that you want to create a class to hold several observable properties as part of one concept, e.g.
class MyState(
foo: Foo,
bar: Bar
) {
var foo by mutableStateOf(foo)
var bar by mutableStateOf(bar)
private set
}
and use it with a single remember
, potentially as part of a default argument expression to allow the state to be hoisted to the caller:
@Composable fun MyComposable(
state: MyState = remember { MyState(defaultFoo, defaultBar) }
) {
Adam Powell
12/25/2020, 10:46 PM@Composable
functions emit UI based on state provided to them via parameters; the more private state you can hoist to be controllable by the caller/testable in isolation, outside of composition, the nicer your API becomes to work with. The pattern of remember
expressions as default arguments above help balance caller convenience with flexibility when they want/need it, as opposed to creating a lot of private state.Timo Drick
12/26/2020, 2:49 PM@Composable
fun <T>rememberState(block: () -> T) = remember { mutableStateOf(block()) }
Adam Powell
12/26/2020, 3:48 PMinline