I have been trying to figure out how to hoist multiple states in 1 single composable/ viewModel but without any luck.
Whenever I type in one of the fields, it types in all fields at the same time. Like this: https://gyazo.com/b3aae51a92a00478dd228f69dc8c12ac
Also, if I add extra parameters to the UI composable (UserProfile), it gives me an error in the hoisted composable since its missing in both Userprofile inside UserProfileState. I know how to hoist 1 state but with mutiple it seems to become more complex. How can I hoist mutiple states in the same composable/ ViewModel ?
My Hoisted state composable:
@Composable
fun UserProfileState() {
var username by remember { mutableStateOf("") }
var email by remember { mutableStateOf("") }
UserProfile(
currentUserName = username,
newTextFieldValueUserName = { username = it })
UserProfile(
currentUserName = email,
newTextFieldValueUserName = { email = it},
)
}
UserProfile containing both Username and Email textfields:
@Composable
fun UserProfile(
currentUserName: String,
newTextFieldValueUserName: (String) -> Unit,
) { // Username and Email is placed here, as seen below!}
Username:
Spacer(modifier = Modifier.height(20.dp))
OutlinedTextField(
value = currentUserName,
onValueChange = newTextFieldValueUserName,
label = { Text(text = "Name") },
singleLine = true,
leadingIcon = {
Icon(
imageVector = Icons.Default.Email,
contentDescription = null
)
},
modifier = Modifier
.fillMaxWidth(180F),
)
Email:
Spacer(modifier = Modifier.height(20.dp))
OutlinedTextField(
value = currentUserName,
onValueChange = newTextFieldValueUserName,
label = { Text(text = "Email") },
singleLine = true,
leadingIcon = {
Icon(
imageVector = Icons.Default.Email,
contentDescription = null
)
},
modifier = Modifier
.fillMaxWidth(180F),
)