[React Native] 03-5 μ¬μ¬μ©ν μ μλ μ»΄ν¬λνΈ λ§λ€κΈ°
FlatList μ½μ΄ μ»΄ν¬λνΈ μ¬μ©νκΈ°
λκ°μ μ»΄ν¬λνΈλ₯Ό μ¬λ¬ κ° λ λλ§ν λλ FlatListλ₯Ό μ¬μ©νλ κ²μ΄ μλκ° λΉ λ₯΄λ€.
import {FlatList} from 'react-native'
<FlatList data={people} />
keyExtractor μμ±
itemκ³Ό index κ°μ΄ λ§€κ°λ³μμΈ μ½λ°± ν¨μλ₯Ό μ§μ ν΄ renderItemμ μ€μ ν μ½λ°± ν¨μκ° λ°ννλ μ»΄ν¬λνΈμ key μμ±μ μ€μ ν κ°μ μ»μ
<FlatList
data={people}
renderItem={({item}) => <Person person={item} />}
keyExtractor={(item, index) => item.id} />
β νμ΄ν ν¨μλ ν¨μλͺ μ΄ μμ΄λ λ¨
β id κ°μ μμ±μ΄ μλ€λ©΄ index.toString()μ λ°νν΄λ λ¨
ItemSeparatorComponent μμ±
μ΄ μμ±μ μ€μ ν μ½λ°± ν¨μκ° λ°ννλ μ»΄ν¬λνΈλ₯Ό μμ΄ν κ³Ό μμ΄ν κ°μ ꡬλΆμ μν μ νλ μ»΄ν¬λνΈλ‘ μ½μ ν¨
ItemSeparatorComponent={() => <View style={styles.itemSeparator} />}
const styles = StyleSheet.create({
itemSeparator: {borderWidth: 1, borderColor: MD2Color.grey500}
})
moment ν¨ν€μ§ κΈ°λ₯ μ¬μ©νκΈ°
Date ν΄λμ€μ μ μ¬νμ§λ§ Date ν΄λμ€κ° μ 곡νμ§ μλ λͺ κ°μ§ μ μ©ν κΈ°λ₯μ μ 곡ν¨
Date μΈμ€ν΄μ€λ₯Ό moment μΈμ€ν΄μ€λ‘ λ°κΎΈλ©΄ λ¨
moment(person.createdDate)
κ³Όκ±°μ νμ¬μ μκ° μ°¨μ΄λ₯Ό μκΈ° μ¬μ΄ ννλ‘ μλ €μ€
moment(person.createDate).startOf('day').fromNow()
β νκ΅μ΄λ‘ μ¬μ©νκ³ μΆλ€λ©΄ moment-with-locales-es6 ν¨ν€μ§μμ moment κ°μ²΄λ₯Ό μ»κ³ moment.locale('ko')
β μ¬νκ» μλ°νμ μ΄λ―Έμ§κ° λλ€μΌλ‘ λμ€μ§ μκ³ κ°μ κ²λ§ λ°λ³΅λλ μ΄μ
export const unsplashUrl = (width: number, height: number): string =>
`https://source.unsplash.com/random/${width}x${height}`
export const avatarUriByName = (name: string) =>
`https://ui-avatars.com/api/?name=${name.split(' ').join('+')}`
μλ util.ts μ μ½λ μΌλΆμ΄λ€. ${}λ₯Ό μ¬μ©νμ¬ λ§ν¬ λ±μ λ³μ λλ ννμμ μ½μ νκ³ μΆμ λλ λ°±ν± λ¬Έμ `` λ‘ κ°μΈμ£Όμ΄μΌ νλλ° λ μ΄κ±Έ μμ λ°μ΄ν ''λ‘ μ°©κ°νμλ€.. . .. .. ^ ^ . . . μ°Έκ³ λ‘ λ§₯μμ λ°±ν± λ¬Έμλ option + ~ λΌκ³ νλ€.
const
"μμ(constant)"λΌκ³ λ ν¨
β ν λ² κ°μ ν λΉνλ©΄ λ€μ λ³κ²½ν μ μλ λ³μλ₯Ό λνλ
νμ μ€ν¬λ¦½νΈ κ΅μ§ν¨ νμ ꡬ문
λμ λ°μ΄ν° νμ (algebraic data type, ADT) : Aμ Bλ₯Ό νμ μ΄λΌκ³ ν λ 'A νμ μ΄κ±°λ Bνμ ' νΉμ 'Aνμ μ΄λ©΄μ B νμ ' κ³Ό κ°μ νμ μ λ§λ€ λ μ¬μ©νλ κ²
ν©μ§ν© β‘οΈ A | B
κ΅μ§ν© β‘οΈ A & B
ComponentProps νμ
β‘οΈ μμ±(props)μ νμ μ λͺ μμ μΌλ‘ μ μνλλ° μ¬μ©
React μ»΄ν¬λνΈ: μμ±(props)μ λ°μμ νλ©΄μ μ΄λ»κ² λ λλ§ν μ§λ₯Ό κ²°μ νλ μμ
μμ±(props): μ»΄ν¬λνΈ μΈλΆμμ μ λ¬λλ©°, μ»΄ν¬λνΈ λ΄λΆμμ μ¬μ©
<μ¬μ©λ²>
μμ±_νμ
= ComponentProps<typeof μ»΄ν¬λνΈ_μ΄λ¦>
μμ¬ μ°μ°μ
β‘οΈ μμ¬ μ°μ°μλ₯Ό μ¬μ©νλ©΄ κ°μ²΄μμ μΌλΆ μμ±μ λΊ λλ¨Έμ§λ₯Ό μ½κ² κ°μ Έμ¬ μ μμ
const person = {
name: "Alice",
age: 30,
country: "Wonderland"
};
const { age, ...rest } = person;
console.log(age); // 30
console.log(rest); // { name: "Alice", country: "Wonderland" }
μ μμ μμ '...rest'λ 'age' μμ±μ μ μΈν λͺ¨λ μμ±μ μλ‘μ΄ κ°μ²΄λ‘ 볡μ¬ν¨.
StyleProp νμ
: μ»΄ν¬λνΈμ μ€νμΌμ μ€μ νλ λ° μ¬μ©ν¨
import { View, StyleProp, ViewStyle } from 'react-native';
type MyComponentProps = {
containerStyle?: StyleProp<ViewStyle>;
};
function MyComponent({containerStyle}: MyComponentProps) {
return (
<View style={containerStyle}>
</View>
);
}
containerStyle?: StyleProp<ViewStyle>
'?' κΈ°νΈλ μ΄ μμ±μ΄ μ νμ μμ λνλ β‘οΈ μ΄ μμ±μ μ§μ νμ§ μμλ λλ©° μλ΅ν μ μμ
ViewStyle : 'View' μ»΄ν¬λνΈμ μ μ©ν μ μλ μ€νμΌμ νμ
Text μ½μ΄ μ»΄ν¬λνΈμ μμ± νꡬ
ν μ€νΈ μ€ μ μ ν
<Text numberOfLines={3}>text</Text>
β‘οΈ Textκ° λ λλ§νλ ν μ€νΈλ₯Ό 3μ€λ‘ μ ν
ν μ€νΈ κΎΈλ―ΈκΈ°
email: {textDecorationLine: 'underline', textDecorationColor: 'blue'},
β‘οΈ textDecorationLine: ν μ€νΈμ λ°μ€
β‘οΈ textDecorationColor: λ°μ€ μμ