miqbaldc
09/15/2021, 3:15 PMmutableStateOf(<with initial/default value>)
but the recomposition unable to use the default valuemiqbaldc
09/15/2021, 3:19 PMfun MyPage(state: MyState) {
// e.g: uiState has value of true
val uiState = state.observeAsState().value // this is an API that returns boolean
TopBar() { // other modifier is omitted
MyScreen(isChecked = uiState)
}
}
@Composable
private fun MyScreen(isChecked: Boolean) {
CheckBox(
isChecked = isChecked
)
}
@Composable
private fun CheckBox(
isChecked: Boolean,
) {
val checkedState = remember { mutableStateOf(isChecked) }
// debuggin the checkedState.value, seems returning "false" instead of "true"
// me trying to update the state
Text(modifier = Modifier.clickable { checkedState.value = !checkedState.value })
}
miqbaldc
09/15/2021, 3:27 PMcheckedState
inside MyPage
or maybe any other alternative suggestion? 🙏
https://dev.to/zachklipp/remember-mutablestateof-a-cheat-sheet-10ma (finish reading this but I’m unable to grasp the idea why it didn’t show the value from parent composable function on this part of code mutableStateOf(<with default value>)
) 😄Zach Klippenstein (he/him) [MOD]
09/15/2021, 3:34 PMmiqbaldc
09/15/2021, 3:53 PM@Composable
fun MyPage(state: MyState) {
var checkedState by remember { mutableStateOf(state.observeAsState().value) }
CheckBox(
isChecked = checkedState,
onCheckedChange = { wasChecked -> checkedState = wasChecked }
)
}
@Composable
private fun CheckBox(
isChecked: Boolean,
onCheckedChange: (Boolean) -> Unit,
) {
// me trying to update the state
Text(modifier = Modifier.clickable { onCheckedChange(!checkedState.value) })
}
Zach Klippenstein (he/him) [MOD]
09/15/2021, 5:36 PMobserveAsState
is a composable function, so can’t be called from a remember
lambda.Zach Klippenstein (he/him) [MOD]
09/15/2021, 5:36 PMclickable
for checkbox, use toggleable
miqbaldc
09/16/2021, 3:10 AMobserveAsState
that’s my apologize, I should’ve assign it to a variable first 😄, e.g:
val uiState = state.observeAsState().value
var checkedState by remember { mutableStateOf(uiState) }
miqbaldc
09/16/2021, 3:11 AM@Composable
private fun TextCheck(
isChecked: Boolean,
) {
val checkedState = remember { mutableStateOf(isChecked) }
// debuggin the checkedState.value, seems returning "false" instead of "true"
Text(text = if (checkedState.value) "Clicked!" else "Not Clicked!")
}
I would like to confirm about this part of code:
val checkedState = remember { mutableStateOf(isChecked) }
seems the initial values (after updated from false
to true
as isChecked
modified from highest level composable), in the other hands checkedState
still using the previous values when doing the recomposition, it’ll show Not Clicked
---
After modifying it to:
@Composable
private fun TextCheck(
isChecked: Boolean,
) {
val checkedState = remember { mutableStateOf(false) }
checkedState.value = isChecked
Text(text = if (checkedState.value) "Clicked!" else "Not Clicked!")
}
The above snippet will changed from Not Clicked!
to Clicked!
(when recomposition)
---
Every example on the material and official docs, seems there’s no convention about this snippet below
Does this an anti-pattern or (intended not possible to have default values from composable param)?
@Composable
fun AbcScreen(isChecked: Boolean) {
val checkedState = remember { mutableStateOf(isChecked) }
🙏Zach Klippenstein (he/him) [MOD]
09/16/2021, 12:18 PMCheckBox
component should always just read the isChecked
value directly, and forward the event handler function. If you’re creating a mutable state that always is synced to the exact value of a parameter, only to then immediately read that state in the composition, there’s no point to having the state at all – just use the parameter directly.miqbaldc
09/16/2021, 5:26 PM