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... ํ๋ก์ ํธ ๋๋ ์ ์จ๋ณผ ์ ์๊ธธ ๋ฐ๋ผ๋ฉฐ.. ์ด๋ฒ ๊ธ์ ๋ง์น๋ค.๐
์ฐธ๊ณ ๊ธ
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
'๐ Studying > ๐ TIL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ReactNative] ์ค์ฒฉ ๋ผ์ฐํฐ ๋ค๋น๊ฒ์ด์ ์ง์ ํด์ ํ์ด์ง ์ด๋ (0) | 2023.01.06 |
---|---|
[ReactNative] React-Query๋ก ๋ฌดํ์คํฌ๋กค ์ ์ฉํ๊ธฐ (1) | 2023.01.05 |
[ReactNative] useEffect์ useFocusEffect (0) | 2023.01.03 |
์ปค์คํ ํ ์ผ๋ก ํฌ๋๋ฆฌ์คํธ input ๋ฆฌํฉํ ๋งํ๊ธฐ (0) | 2023.01.02 |
[ReactNative] AsyncStorage๋ก ๋ฐ์ดํฐ ์ ์ฅํ๊ธฐ (0) | 2022.12.30 |
Comment