@Composable
fun StateStateHoisting() {
var pyeong by rememberSaveable {
mutableStateOf("23")
}
var squaremeter by rememberSaveable {
mutableStateOf((23 * 3.306).toString())
}
Column(modifier = Modifier.padding(16.dp)) {
OutlinedTextField(
value = pyeong,
onValueChange = {
if (it.isBlank()) {
pyeong = ""
squaremeter = ""
return@OutlinedTextField
}
val numericValue = it.toFloatOrNull() ?: return@OutlinedTextField
pyeong = it
squaremeter = (numericValue * 3.306).toString()
},
label = { Text(text = "평") }
)
OutlinedTextField(
value = squaremeter,
onValueChange = {
},
label = { Text(text = "제곱미터") }
)
}
}
@Composable
fun StateStateHoisting() {
var pyeong by rememberSaveable {
mutableStateOf("23")
}
var squaremeter by rememberSaveable {
mutableStateOf((23 * 3.306).toString())
}
PyeongToSquareMeterStateless(pyeong = pyeong, squareMeter = squaremeter) {
if (it.isBlank()) {
pyeong = ""
squaremeter = ""
return@PyeongToSquareMeterStateless
}
val numericValue = it.toFloatOrNull() ?: return@PyeongToSquareMeterStateless
pyeong = it
squaremeter = (numericValue * 3.306).toString()
}
}
@Composable
fun PyeongToSquareMeterStateless(
pyeong: String,
squareMeter: String,
onPyeongChange: (String) -> Unit
) {
Column(modifier = Modifier.padding(16.dp)) {
OutlinedTextField(
value = pyeong,
onValueChange = {
onPyeongChange
},
label = { Text(text = "평") }
)
OutlinedTextField(
value = squareMeter,
onValueChange = {
},
label = { Text(text = "제곱미터") }
)
}
}
//단방향 데이터 흐름
@Composable
fun FunctionA(){
var switchState by remember {
mutableStateOf(true)
}
val onSwitchChange = {value : Boolean ->
switchState = value
}
FunctionB(
switchState,
onSwitchChange
)
}
@Composable
fun FunctionB(switchState : Boolean, onSwitchChange : (Boolean) -> Unit){
Switch(checked = switchState, onCheckedChange = onSwitchChange)
}
FunctionA()에 이벤트 핸들러 선언과 자식 컴포저블에 상탯값을 FunctionB의 파라미터로 전달한다. FunctionB 안의 Switch는 스위치가 변경될 때마다 해당 이벤트 핸들러를 호출하고, 현재 설정값을 전달하도록 설정된다.

위 사진과 같이 단방향으로 데이터의 흐름이 진행된다.
예제 코드
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Chap20_StateExampleTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
DemoScreen()
}
}
}
}
}
@Composable
fun DemoScreen() {
//1.
//var textState = remember { mutableStateOf("") }
//2.
//위와 같은 방법이지만 좀 더 간결한 접근 방식인 by 키워드를 사용.
//by 키워드를 통해 코틀린 프로퍼티를 위임하는 것이다.
//by 키워드를 사용하면 .value를 사용하지 않아도 된다.
// var textState by remember {
// mutableStateOf("")
// }
//3. MutableState 객에체 대한 접근을 값과 세터 함수로 바꾸기
var (textValue, setText) = remember {
mutableStateOf("")
}
val onTextChange = {text : String ->
//1. textState.value = text
//2. textState = text
setText(text)
}
MyTextField(textValue, onTextChange)
}
@Composable
fun MyTextField(textValue : String, onTextChange : (String) -> Unit){
//1. TextField(value = textState.value, onValueChange = onTextChange)
//2. TextField(value = textState, onValueChange = onTextChange)
TextField(value = textValue, onValueChange = onTextChange)
}
//단방향 데이터 흐름
@Composable
fun FunctionA(){
var switchState by remember {
mutableStateOf(true)
}
val onSwitchChange = {value : Boolean ->
switchState = value
}
FunctionB(
switchState,
onSwitchChange
)
}
@Composable
fun FunctionB(switchState : Boolean, onSwitchChange : (Boolean) -> Unit){
Switch(checked = switchState, onCheckedChange = onSwitchChange)
}
@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
Chap20_StateExampleTheme {
DemoScreen()
FunctionA()
}
}