Text is always “my age is 0” (never changed). why?...
# compose
t
Text is always “my age is 0” (never changed). why? it’s bug?
Copy code
class MainActivity : ComponentActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent {
        var age by remember { mutableStateOf(0) }
        val person by remember {
          derivedStateOf { "my age is $age" }
        }
        Column(modifier = Modifier.padding(16.dp))  {
          Button(onClick = {
            age += 1
            Log.e("person", person)
          }) {

            Text(text =   person)
          }
          Text(
            text = person)
        }
    }
  }
}
h
Content of remember is only execute once without a specific key
Copy code
val person by remember(key = age) {
   derivedStateOf { "my age is $age" }
}
+ maybe you don't have to use a derivedState for this case
z
Yeah you don’t need derivedState for this
g
It looks like a bug, cos it works for me (compose-ui: 1.2.0), no change made to code.
And it should work, since from my understanding:
Copy code
var stateVariable by remember {
        derivedStateOf {
            // any state read within this block will cause this block
            // to be re-executed.
        }
    }
But you right, that in this case, you don't really need derrivedStateOf.
t
Thanks. guys. (Especially Gordon) It works in 1.2.0, but doesnt work 1.3.0. And….It works in 1.3.1. ^^ maybe it was a bug.
a
Anything in a remember must be passed down explicitly from my experience otherwise it’ll just remember it once. Since you wrap the derived state
It’s not receiving new value from the remember memo