# ์—๋Ÿฌ ํ•ด๊ฒฐ๐Ÿ”‘ Styled Component๋กœ FlatLst ์‚ฌ์šฉ ์‹œ renderItem type unknown ์—๋Ÿฌ

FlatList์— ํƒ€์ž…์ด ์ •์˜๋œ data๋ฅผ ๋„˜๊ฒจ์ฃผ์—ˆ์Œ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ , renderItem์— ํƒ€์ž…์ด unknown์œผ๋กœ ๋‚˜์˜ค๋Š” ์—๋Ÿฌ๊ฐ€ ์žˆ์—ˆ๋‹ค. styled.FlatList์ด styled-components์˜ ํƒ€์ž… ์ •์˜๋กœ๋Š” ์ง€์›๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด์—ˆ๋‹ค.

const [cleanData, setCleanData] = useState<CoinItem[]>([]);

// ์ค‘๋žต
return (
  <List
    data={cleanData} // const cleanData: CoinItem[]
    ItemSeparatorComponent={() => <View style={{ height: 10 }} />}
    numColumns={3}
    columnWrapperStyle={{
      justifyContent: 'space-between',
    }}
    keyExtractor={(item) => item.id} // error: Object is of type 'unknown'.ts(2571)
    renderItem={({ item, index }) => (
      <Coin index={index} symbol={item.symbol} /> // error: Object is of type 'unknown'.ts(2571)
    )}
  />
);

const List = styled.FlatList`
  padding: 20px 10px;
  width: 100%;
`;

์‚ฌ์‹ค.. type assertion์œผ๋กœ keyExtractor={(item) => (item as CoinItem).id} ์ด๋ ‡๊ฒŒ ์ •์˜ํ•ด์ค€๋‹ค๋ฉด ์ œ์ผ ๊ฐ„๋‹จํ•˜๊ฒŒ ํƒ€์ž…์„ ์ •์˜ํ•ด์ค„ ์ˆ˜ ์žˆ๊ธด ํ•˜์ง€๋งŒ, item์˜ key๋“ค์„ ์ž์ฃผ ์จ์ฃผ์–ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ์—” ์ผ์ผํžˆ assertํ•ด์ฃผ์–ด์•ผ ํ•˜๋‹ˆ ์ข‹์€ ๋ฐฉ๋ฒ•์€ ์•„๋‹ˆ๋‹ค.

solution 1) ์Šคํƒ€์ผ๋“œ ์ปดํฌ๋„ŒํŠธ์— ํƒ€์ž… ๋ถ€์—ฌ

const List = (styled.FlatList`
  padding: 20px 10px;
  width: 100%;
` as unknown) as typeof FlatList;

์Šคํƒ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋ฉด ๊ฐ€์žฅ ๋งŽ์ด ๋‚ด๋†“๋Š” ํ•ด๊ฒฐ์ฑ…์ด์ง€๋งŒ idealํ•œ ๋ฐฉ๋ฒ•์€ ์•„๋‹Œ ๊ฒƒ ๊ฐ™๋‹ค. ์ •ํ™•ํžˆ๋Š” ์Šคํƒ€์ผ๋“œ ์ปดํฌ๋„ŒํŠธํ™”๋œ FlatList์— unknown์„ ๋ถ€์—ฌํ•œ ๋’ค List = (styled.FlatList~~as unknown) ๊ทธ์— ๋Œ€ํ•œ ํƒ€์ž…์„ ๋‹ค์‹œ FlatList๋กœ ๊ฐˆ์•„๋ผ์šฐ๋Š” ๋ฐฉ์‹์ธ๋ฐ ์˜.. ๋งˆ๋œฉ์ž–๋‹ค.

solution 2) ์ตœ์ข…_์ง„์งœ์ตœ์ข…_์ด๊ฒŒ์ง„์งœ์ตœ์ข….jpg ์—ฌ๊ธฐ (opens new window) ๊ทธ๋ฆฌ๊ณ  ์—ฌ๊ธฐ (opens new window)์—์„œ ์ฐธ๊ณ ํ•œ ๋ฐฉ์‹์ด๋‹ค.' List = styled(FlatList)๋กœ ์„ ์–ธํ•˜๋˜, generic์œผ๋กœ ๋ Œ๋” ์•„์ดํ…œ์˜ ํƒ€์ž…์„ ์ „๋‹ฌํ•ด์ฃผ๋Š” ๋ฐฉ์‹.

const List = styled(FlatList as new () => FlatList<CoinItem>)`
  padding: 20px 10px;
  width: 100%;
`;

์ฐธ๊ณ ๋กœ ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•๋„ ์žˆ๋Š”๋ฐ, ์œ„์˜ ๋ฐฉ๋ฒ•์ด ๋” ๊ฐ„ํŽธํ•˜๋‹ค.

const StyledGenericComponent = styled(GenericComponent)`
  position: relative;
`  as React.ComponentType as new <T>() => GenericComponent<T>;
Last Updated: a year ago