[TIL] ๐ŸŒผ24/05/03๐ŸŒผ#mutableStateOf vs mutableStateListOf

0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
95/104
post-thumbnail
post-custom-banner

[TIL] ๐ŸŒผ24/05/03๐ŸŒผ#mutableStateOf vs mutableStateListOf

mutableStateOf vs mutableStateListOf

๐Ÿ“Œ์ฐธ๊ณ ์ž๋ฃŒ

  • mutableStateOf<List<T>>๋Š” immutable list ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•  ๋•Œ ์‚ฌ์šฉํ•ด์•ผ
    • list reference๊ฐ€ ๋ณ€๊ฒฝ๋˜์—ˆ์„ ๋•Œ recomposition ํŠธ๋ฆฌ๊ฑฐ๋จ
    • list๊ฐ€ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š์Œ์ด ๋ณด์žฅ๋  ๋•Œ ์‚ฌ์šฉ (ex. Room database)
val state = remember { mutableStateOf(mutableListOf("A", "B", "C")) }
state.value.add("D") // This won't trigger recomposition
  • mutable list ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•  ๋•, mutableStateListOf<T> ์‚ฌ์šฉ
val mutableList = remember { mutableStateListOf("A", "B", "C") }
state.add("D") // This will trigger recomposition
  • ์ถ”๊ฐ€๋กœ, Primitive ํƒ€์ž…์˜ ์ƒํƒœ ๊ด€๋ฆฌ์—” mutableDoubleStateOf, mutableFloatStateOf, mutableIntStateOf, mutableLongStateOf ์‚ฌ์šฉ
    • mutableStateOf์™€ ๋™์ผํ•˜์ง€๋งŒ, JVM ํ”Œ๋žซํผ(ex. Android)์—์„œ autoboxing ๋ฐฉ์ง€ํ•ด์คŒ
      -> more memory efficient
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€