Vishnu Shrikar
10/26/2023, 6:22 AMvar field by remember (mutableStateOf(initialState))
where initial state is passed into the class on construction. I then construct a new instance of this class and the problems begin. It recieves a new version of initialState (as it should because I passed in a different one) but then the delegation just keeps the old value. It is incredibly weird because this is a completely new object. Any ideas?ascii
10/26/2023, 6:39 AMremember
has no knowledge of changes outside it unless you explicitly tell it there are changes:
remember(initialState) { mutableStateOf(initialState) }
Generally though, you shouldn't want this behaviour because now you have two sources of truth. One via the initial param, and one is the remembered state you're creating.field
later on? If yes, when does parent change initialState
? Don't you think these should be merged so that changes are done only in one place?
For example, you could do this:
@Composable
Parent() {
var value by remember { mutableStateOf(initialValue) }
Child(value, { value = it })
}
@Composable
Child(value: Any, updateField: (Any) -> Unit) {
// Fire off an update for whatever reason
// Parent updates state variable which will recompose child
updateField(newValue)
}
Vishnu Shrikar
10/26/2023, 3:34 PM