elye
10/05/2022, 2:24 AM@Composable
fun MyComposableFun(textValue: String) {
val newTextValue = remember(textValue) { mutableStateOf(textvalue) }
val myComposeView = remember {
MyComposable().apply {
setContent { Text(newTextValue) }
}
}
Column(horizontalAlignment = Alignment.CenterHorizontally) {
Text("Title: $newTextValue")
myComposeView.myComposable?.invoke()
}
}
class MyComposable {
var myComposable: (@Composable () -> Unit)? = null
fun setContent(content: @Composable () -> Unit) {
myComposable = content
}
}
On initial load it's okay. But when textValue changed, and the function got called again,
• The Text("Title: $newTextValue") got updated - which is good
• The setContent { Text(newTextValue) } don't get updated. Why?
Did I get anything wrong?Stylianos Gakis
10/05/2022, 7:30 AMremember there and you’re not passing any keys to it, which means that it will run once and never again. Although I am curious to see how the insides of MyComposable looks like, it looks quite odd from looking at it like this.elye
10/05/2022, 7:31 AMkey to it, it will reconstruct the entire MyComposable. I try to avoid MyComposable to be reconstructed.elye
10/05/2022, 7:33 AM@Composable
fun MyComposableFun(textValue: String) {
val newComposeView = @Composable { Text(textValue) }
val myComposeView = remember {
MyComposable().apply {
setContent (newComposeView)
}
}
Column(horizontalAlignment = Alignment.CenterHorizontally) {
Text("Title: $textValue")
myComposeView.myComposable?.invoke()
}
}
class MyComposable {
var myComposable: (@Composable () -> Unit)? = null
fun setContent(content: @Composable () -> Unit) {
myComposable = content
}
}elye
10/05/2022, 7:34 AM@Composable
fun MyComposableFun(textValue: String) {
fun newComposeView() = @Composable { Text(textValue) }
val myComposeView = remember {
MyComposable().apply {
setContent (newComposeView())
}
}
Column(horizontalAlignment = Alignment.CenterHorizontally) {
Text("Title: $textValue")
myComposeView.myComposable?.invoke()
}
}
class MyComposable {
var myComposable: (@Composable () -> Unit)? = null
fun setContent(content: @Composable () -> Unit) {
myComposable = content
}
}Stylianos Gakis
10/05/2022, 11:39 AMThanks. If we pass the key to it, it will reconstruct the entire MyComposable. I try to avoid MyComposable to be reconstructed.Yes, I’m just saying why I think it doesn’t update. This structure in general is a bit odd with creating a class which then contains the function instead of having the function itself, but can you try (in the original snippet) doing smth like
val updatedNewTextValue = rememberUpdatedState(newTextValue) and pass that inside your composable instead of passing newTextValue directly and see if this changes anything?