[ReactNative] <FlatList> ์™€ <ScrollView>
728x90

Today, What I Learned?

๋ฆฌ์•กํŠธ ๋„ค์ดํ‹ฐ๋ธŒ ๊ณต์‹๋ฌธ์„œ์˜ ScrollView๋ฅผ ๋ณด๋‹ค๊ฐ€ <FlatList/> ์™€์˜ ๋น„๊ตํ•˜๋Š” ๋ถ€๋ถ„์ด ์žˆ์–ด ์ •๋ฆฌํ•ด ๋ณด๊ณ ,

ํ˜„์žฌ ๋งŒ๋“ค๊ณ  ์žˆ๋Š” ์˜ํ™” ์•ฑ์˜ ScrollView ๋ถ€๋ถ„์„ FlatList๋กœ ๋ฐ”๊ฟ”๋ณด๋Š” ์ž‘์—…์„ ๊ธฐ๋กํ•ด๋‘๋ ค ํ•œ๋‹ค.

 

 

<ScrollView> vs <FlatList>

์œ„ ์บก์ฒ˜๋Š” ๋ฆฌ์•กํŠธ ๋„ค์ดํ‹ฐ๋ธŒ ๊ณต์‹ ๋ฌธ์„œ์—์„œ ๋ฐœ์ทŒํ•ด์˜จ ๋ถ€๋ถ„์ด๋‹ค.

 

์‚ดํŽด๋ณด๋ฉด ๋Œ€๋žต ์ด๋Ÿฐ ๋‚ด์šฉ์ด๋‹ค.

  • ScrollView๋Š” ๋ Œ๋” ์‹œ์— ๋ชจ๋“  ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋“ค์„ ํ•œ ๋ฒˆ์— ๊ฐ€์ ธ์™€์„œ ์„ฑ๋Šฅ์ด ์ €ํ•˜๋œ๋‹ค.
  • FlatList๋Š” ๋ Œ๋” ์‹œ์— ๋ณด์ด๋Š” ๊ฒƒ๋“ค๋งŒ lazilyํ•˜๊ฒŒ ๊ฐ€์ ธ์˜ค๊ณ , ์Šคํฌ๋กค์„ ๋‚ด๋ ค์•ผ ๋ณผ ์ˆ˜ ์žˆ๋Š” ์•„์ดํ…œ๋“ค์€ ์ง€์›Œ๋ฒ„๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— ๋ฉ”๋ชจ๋ฆฌ์™€ ์ฒ˜๋ฆฌ ์‹œ๊ฐ„์„ ์ ˆ์•ฝํ•  ์ˆ˜ ์žˆ๋‹ค.

 

<FlatList/>

FlatList๋Š” ์—ฌ๋Ÿฌ ๊ธฐ๋Šฅ์„ ๋‚ด์žฅํ•˜๊ณ  ์žˆ๋‹ค.

  • ํ—ค๋” / ํ‘ธํ„ฐ ์ง€์›
  • horizontal mode ์ง€์›
  • ๋‚ด๋ ค ๋‹น๊ฒผ์„ ๋•Œ์˜ Refresh ์ง€์›
    ๋“ฑ๋“ฑ...

๊ฐ€์žฅ ํฐ ํŠน์ง•์€ map ๋ฉ”์„œ๋“œ๋ฅผ ๋‚ด์žฅํ•˜๊ณ  ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.


๊ทธ๋ž˜์„œ ํ•„์ˆ˜์ ์œผ๋กœ ์ „๋‹ฌ๋˜์–ด์•ผ ํ•˜๋Š” props๊ฐ€ ์žˆ๋‹ค.

  • data : ๋ Œ๋”๋ง ๋˜์–ด์•ผ ํ•  ๋ฐฐ์—ด
  • renderItem : ์–ด๋–ป๊ฒŒ ๋ Œ๋”๋ง ํ•  ๊ฒƒ์ธ์ง€? (map ๋ฉ”์„œ๋“œ์˜ ์ฝœ๋ฐฑํ•จ์ˆ˜ ๋ถ€๋ถ„์— ํ•ด๋‹น)
    • ์ฝœ๋ฐฑํ•จ์ˆ˜ ๋‚ด์—์„œ ๋งค๊ฐœ๋ณ€์ˆ˜ ์ด๋ฆ„์€ item ์ด์–ด์•ผ ํ•œ๋‹ค๋Š” ๊ทœ์น™์ด ์žˆ๋‹ค!
  • keyExtractor : map ๋ฉ”์„œ๋“œ์˜ key ๋ถ€๋ถ„

 

์ด์ œ ์ฝ”๋“œ๋ฅผ ๋ฆฌํŒฉํ† ๋ง ํ•ด๋ณด๋Š” ๊ณผ์ •!

 

๊ธฐ์กด ์ฝ”๋“œ

  <ScrollView>
      ...
      <ScrollView>
        {data?.videos?.results.map((item) => (
          <VideoWrapper onPress={() => onPress(item.item.key)}>
            <AntDesign name="youtube" size={24} color={isDark ? '#fff' : '#333'} />
            <VideoTitle numberOfLines={1}>{item.item.name}</VideoTitle>
          </VideoWrapper>
        ))}
      </ScrollView>
    </ScrollView>

ScrollView ์•ˆ์— ๋˜๋‹ค๋ฅธ ScrollView๊ฐ€ ์žˆ๋Š” ์ƒํƒœ.

VideoWrapper ์•ˆ์— ๋“ค์–ด๊ฐ€๋Š” ๋ฐ์ดํ„ฐ๋“ค์€ ๊ฐ๊ฐ ๋‹ค๋ฅด๊ธด ํ–ˆ์ง€๋งŒ, ๋ฐ์ดํ„ฐ์˜ ์ˆ˜๊ฐ€ 15๊ฐœ ๊ฐ€๊นŒ์ด ๋˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์—ˆ๋‹ค.

 

์ด์ œ FlatList๋กœ ์ข€ ๋” ํšจ์œจ์ ์ด๊ฒŒ ๋ฆฌํŒฉํ† ๋ง ํ•ด๋ณด๊ธฐ ๐Ÿค”

 

์ˆ˜์ •ํ•œ ์ฝ”๋“œ

  <FlatList
      ListHeaderComponent={
        <>
          ...
        </>
      }
      data={data?.videos?.results}
      renderItem={(item) => <VideoItem item={item.item} onPress={openYoutube} />}
      keyExtractor={(item) => item.id}
      ItemSeparatorComponent={<View style={{ marginBottom: 6 }} />}
    />

๋ Œ๋”๋ง ํ•  ๋ถ€๋ถ„์„ ์ œ์™ธํ•œ ์œ„์ชฝ ์ฝ”๋“œ๋“ค์€ ListHeaderComponents๋ผ๋Š” props์— ๋„ฃ์–ด์ฃผ๋ฉด ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.
๋˜ ์ˆ˜์ •ํ•˜๊ณ  ๋‚˜๋‹ˆ renderItem ์•ˆ์— ๋“ค์–ด๊ฐ€๋Š” ์ฝ”๋“œ์˜ ์–‘์„ ์ค„์—ฌ๋‹ฌ๋ผ๋Š”.. ๊ทธ๋Ÿฐ ๊ฒฝ๊ณ ๊ฐ€ ๋– ์„œ ํ•ด๋‹น ๋ถ€๋ถ„์„ ์ปดํฌ๋„ŒํŠธํ™” ํ–ˆ๋‹ค.

 

 

๋˜ ์ฝ”๋“œ๋ฅผ ์งœ๋‹ค ๋ณด๋‹ˆ ์ด๋Ÿฐ ์—๋Ÿฌ๋„ ๋งŒ๋‚ฌ์—ˆ๋‹ค.

VirtualizedLists should never be nested inside plain ScrollViews with the same orientation - use another VirtualizedList-backed container instead.

์ด ์—๋Ÿฌ๋Š” ScrollViews ์•ˆ์— FlatList๋ฅผ ์“ธ ์ˆ˜ ์—†๋‹ค๋Š” ์˜ค๋ฅ˜์˜€๋‹ค.


VisualizedLists์˜ ์ข…๋ฅ˜๋กœ SectionList์™€ FlatList๊ฐ€ ์žˆ๋Š”๋ฐ, ์ด ๋ฆฌ์ŠคํŠธ๋“ค์€ ๋ชจ๋‘ ์ฝ˜ํ…์ธ  ์–‘์ด ๋งŽ์€ ๋ฆฌ์ŠคํŠธ๋“ค์„ ๋ Œ๋”๋ง ํ•  ๋•Œ ๋ฉ”๋ชจ๋ฆฌ ์†Œ๋น„๋ฅผ ๊ฐ์†Œ์‹œํ‚ค๊ณ  ํผํฌ๋จผ์Šค๋ฅผ ํ–ฅ์ƒํ•œ๋‹ค.

์ˆœ์ˆ˜ํ•œ ScrollViews ์•ˆ์— List๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ํ˜„์žฌ ์Šคํฌ๋ฆฐ์—์„œ์˜ ๋ฉ”๋ชจ๋ฆฌ ๊ณ„์‚ฐ์ด ์–ด๋ ค์›Œ์ง€๊ธฐ ๋•Œ๋ฌธ์— ์ด๋Ÿฐ ์—๋Ÿฌ๊ฐ€ ๋œจ๋Š” ๊ฒƒ ๊ฐ™๋‹ค.

 

 

  <FlatList
    ...
      ListHeaderComponent={
        <>
        ...
          <FlatList
            horizontal
            showsHorizontalScrollIndicator={false}
            contentContainerStyle={{ paddingHorizontal: 20 }}
            ItemSeparatorComponent={<View style={{ width: 10 }} />}
            data={topLatedMovies}
            renderItem={({ item }) => <TopMovieItem movie={item} />}
            keyExtractor={(item) => item.id}
          />
          <Title>UpComing Movies</Title>
        </>
      }
    ...
    />

๊ทธ๋ž˜์„œ ์œ„์™€ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ์ค‘์ฒฉํ•˜์—ฌ ์‚ฌ์šฉํ–ˆ๋‹ค.

 

 

 

๋ช‡ ๋ฒˆ ์‚ฌ์šฉํ•˜๋‹ค ๋ณด๋‹ˆ ํ•œ๊ฒฐ ์ต์ˆ™ํ•ด์ง„ ๋“ฏํ•œ FlatList... ํ”„๋กœ์ ํŠธ ๋•Œ๋„ ์ž˜ ์จ๋ณผ ์ˆ˜ ์žˆ๊ธธ ๋ฐ”๋ผ๋ฉฐ.. ์ด๋ฒˆ ๊ธ€์„ ๋งˆ์นœ๋‹ค.๐Ÿ˜‡

 

 

์ฐธ๊ณ ๊ธ€

https://javascript.plainenglish.io/how-to-fix-virtualizedlists-should-never-be-nested-inside-plain-scrollviews-warning-3a2a887b4ea0

 

How to Fix ‘VirtualizedLists should never be nested inside plain ScrollViews’ Warning

How to Fix ‘VirtualizedLists should never be nested inside plain ScrollViews’ Warning

javascript.plainenglish.io

 

728x90