[TIL] ๐ŸŒผ24/05/29๐ŸŒผ

0

TIL

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

[TIL] ๐ŸŒผ24/05/29๐ŸŒผ

Today I Solved

Today I Read

๐Ÿ“Œ12 ways to improve your Jetpack Compose composable functions

  • modifier ํŒŒ๋ผ๋ฏธํ„ฐ
    • Composable ํ•จ์ˆ˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๊ผญ ์ถ”๊ฐ€ํ•˜๊ธฐ
    • Default ๊ฐ’ ์„ค์ •ํ•˜๊ธฐ (modifier = Modifier)
    • ํ•˜์œ„ Composable์—์„œ modifier ์žฌ์‚ฌ์šฉ X
      // Don't do this
      @Composable
      private fun MyComposable(modifier: Modifier = Modifier) {
       Column(modifier) {
         Text(modifier.clickable(), ...)
         Image(modifier.size(), ...)
         Button(modifier, ...)
       }
      }
      // Do this
      @Composable
      private fun MyComposable(modifier: Modifier = Modifier) {
       Column(modifier) {
         Text(Modifier.clickable(), ...)
         Image(Modifier.size(), ...)
         Button(Modifier, ...)
       }
      }
  • ViewModel ํŒŒ๋ผ๋ฏธํ„ฐ
    • Composable ๋ธ”๋ก ๋‚ด๋ถ€์—์„œ ์ •์˜ X, ์ƒ์„ฑ์ž ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ Inject ํ•˜๊ธฐ
    • viewmodel ๊ฐ์ฒด ๊ทธ๋Œ€๋กœ ํ•˜์œ„ Composable์— ๋„˜๊ธฐ์ง€ X
      // Don't do this
      @Composable
      fun MyComposable(
        viewModel: MyViewModel = viewModel()
      ) {
       OtherComposable(viewModel)
      }
      // Do this
      @Composable
      fun MyComposable(
       viewModel: MyViewModel = viewModel()
      ) {
       val uiState = viewModel.uiState.collectAsState()
       OtherComposable(
         data = uiState.data,
         onSaveButtonClicked = viewModel::onSaveButtonClicked
       )
      }
  • ํŒŒ๋ผ๋ฏธํ„ฐ ์ˆœ์„œ ์ง€ํ‚ค๊ธฐ
    • ํ•„์ˆ˜ ํŒŒ๋ผ๋ฏธํ„ฐ ๋จผ์ €, ๊ทธ ๋‹ค์Œ์— ์„ ํƒ ํŒŒ๋ผ๋ฏธํ„ฐ(= default value๊ฐ€ ์žˆ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ)
    • ์„ ํƒ ํŒŒ๋ผ๋ฏธํ„ฐ๋“ค ์ค‘ modifier๊ฐ€ ๊ฐ€์žฅ ๋จผ์ €
  • Composable ํ•จ์ˆ˜ ํ•˜๋‚˜๋Š” ๋ ˆ์ด์•„์›ƒ์˜ ํ•˜๋‚˜์˜ content๋งŒ emit ํ•ด์•ผ
    //Don't do this
    Column {
     MyComposable()
    }
    //Do this
    @Composable
    private fun MyComposable() {
     Text(...)
     Image(...)
     Button(...)
    }
    // Or do this
    @Composable
    private fun ColumnScope.MyComposable() {
     Text(...)
     Image(...)
     Button(...)
    }
  • Preview Composable์€ private์œผ๋กœ ์„ ์–ธํ•˜๊ธฐ

๐Ÿ“ŒDo you mark your Compose preview functions as private?

  • Preview Composable์„ private์œผ๋กœ ์„ ์–ธํ•ด์•ผ ํ•˜๋Š” ์ด์œ ?
    ํ•ด๋‹น ํด๋ž˜์Šค/ํŒŒ์ผ์—์„œ๋งŒ ์ ‘๊ทผ ๊ฐ€๋Šฅ
    -> ํ”„๋กœ์ ํŠธ์˜ ๋‹ค๋ฅธ ํด๋ž˜์Šค/ํŒŒ์ผ์—์„œ Preview ํ•จ์ˆ˜ modifyํ•˜๋Š” ๊ฒƒ ๋ง‰์Œ
    -> ์บก์Šํ™”, ์—๋Ÿฌ ๊ฐ€๋Šฅ์„ฑ ๋‚ฎ์•„์ง
post-custom-banner

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

comment-user-thumbnail
2024๋…„ 6์›” 13์ผ

์ปดํฌ์ฆˆ ์–ด๋ ต๋˜๋ฐ ใ… ใ… 

1๊ฐœ์˜ ๋‹ต๊ธ€