galex
08/03/2020, 7:00 AMmutableStateOf<User>
to the children as parameter so some Composables
in the hierarchy can update that state so that all the hierarchy will be recomposed? In this case, I have a screen/composable that will sign-in the user, and when the user is signed in I’d like to update the rest of the hierarchy that it happened. The goal is to show all my screens if a user signed-in or not (I’m using compose-router
for the backstack)jim
08/03/2020, 1:24 PMmutableStateOf
for each edge of the graph.
As the type of operations you want to perform start becoming more complex touching multiple objects in your object graph, you may want to consider a more event-based architecture like Flux (https://facebook.github.io/flux/docs/in-depth-overview/#:~:text=Flux%20is%20the%20application%20architecture,a%20lot%20of%20new%20code.). But just keep this in your back pocket. You don't necessarily need a heavy-weight solution yet (and might never need it). Your initial solution of passing down a`mutableStateOf<User>` is a perfectly reasonable start.Fudge
08/03/2020, 1:26 PMjim
08/03/2020, 1:33 PMmutableStateOf<User>
near the root, and that is the single source passed down.brandonmcansh
08/03/2020, 1:35 PMFudge
08/03/2020, 1:36 PMThis is not meaningfully different from state hosting, where a mutable object is passed down, which acts as a container for the composable's internal state.Yes I think passing any kind of mutable object means you can break unidirectional data flow
mutableState
, so it doesn't count as the child giving the parent state?jim
08/03/2020, 1:42 PMFudge
08/03/2020, 1:53 PM@Composable fun Parent() {
val text = state { "parent text" }
Text(text.value)
Child(text)
}
@Composable fun Child(state: MutableState<String>) {
Button(onClick = {state = "child text"}) {
Text("click")
}
}
In this example has the child not reached out and changed the text state of the parent?jim
08/03/2020, 2:07 PM@Composable fun Parent() {
val ee = remember { EventEmitter() }
val text = ee.store.getText()
Text(text.value)
Child(text)
}
@Composable fun Child(text: String, eventEmitter: EventEmitter) {
Button(onClick = {eventEmitter.emit(new ButtonClicked())}) {
Text("click")
}
}
Fudge
08/03/2020, 2:08 PMemit()
mutates EventEmitter
in some way to cause getText()
to return a different valuejim
08/03/2020, 2:11 PMFudge
08/03/2020, 2:13 PMTextFieldValue
and be done with it?jim
08/03/2020, 2:21 PMScrollState
Fudge
08/03/2020, 2:23 PMjim
08/03/2020, 2:28 PMFudge
08/03/2020, 2:30 PMjim
08/03/2020, 2:47 PMgalex
08/03/2020, 5:14 PM