React Naive에서 제공하는 컴포넌트는 프로젝트 구동 시 각 운영체제에 맞는 컴포넌트로 변환된다
Web | React Native | Android | IOS |
---|---|---|---|
<div> | <View> | android.View | UIView |
<input> | <TextInput> | EditText | UITextField |
… | … | … | … |
아래 컴포넌트는 기본적으로 ‘react-native’에서 제공 됨.
import { ... } from 'react-native'
전체 컴포넌트는 공식 문서 참고
<View>Hello World</View> // ❌Error!
<SafeAreaView>
...
</SafeAreaView>
<View>
<Text>Hello World</Text>
</View>
<Button>Click!</Button> // ❌Error! 내부에 직접 컨텐츠를 넣을 수 없음
<Button title="Click!" style={{color: "red"}} /> // ❌Error! 스타일 속성 미지원
<Button title="Click!" onPress={() => console.log("click!")} /> // ✅Correct!
<Pressable\>
참고export default function Button({onPress, title}) {
return (
<Pressable style={{padding: 6}} onPress={onPress}>
<Text style={{color: "#000"}}>{title}</Text>
</Pressable>);
}
onChangeText()
: 매개변수로 input value값 제공<TextInput
placeholder="text me!"
onChangeText={(value) => console.log(value)}
/>
<TextInput
keyboardType="number-pad"
autoCorrect={false} // 자동수정
/>
keyboardType
default
| number-pad
|decimal-pad
| numeric
| email-address
| phone-pad
autoCapitalize
characters
(모든 문자) | words
(단어의 첫 글자) | sentences
(문장의 첫 글자) | none
autoCorrect
<Image source={require("../assets/image.png")} />
<View>
와 <Image>
태그의 조합으로 만들어짐.source
: 이미지 경로style
: 이미지의 컨테이너 (View
태그)에 적용 할 스타일imageStyle
: 이미지에 적용 할 스타일 <ImageBackground
source={require("./background.png")}
style={styles.screen} // View컴포넌트에 적용
imageStyle={styles.backgroundImage} // Image컴포넌트에 적용
resizeMode="cover"
>
...
</ImageBackground>
// ImageBackground 내부 코드. View와 Image의 조합
// 스타일이 각각 전달 됨.
<View style={props.style}>
<Image style={props.imageStyle} />
</View>
<ScrollView\>
를 감싸는 <View\>
컴포넌트를 활용해 높이값을 설정한다.<FlatList\>
사용 권장// ❌
<ScrollView style={{height: "300px"}}>...</ScrollView>
// ✅
<View style={{height: "300px"}}>
<ScrollView>
...
</ScrollView>
</View>
data
true
renderItem
true
keyExtractor
false
const itemData = [
{
id: 1,
text: 'A'
},
{
id: 2,
text: 'B'
},
{
id: 3,
text: 'C'
},
]
<View style={{height: "300px"}}>
<FlatList
data={itemData}
renderItem={item => (
// 항목에 key 속성을 전달 할 필요가 없음.
<View>{item.text}</View>
)}
keyExtractor={item => item.id} // 기본값 : item.key
/>
</View>
<Pressable
onPress={() => console.log("Clicked!")}
style={({pressed}) => ( // pressed: boolean => 컴포넌트가 선택(pressed)상태일 때 true
{backgroundColor: pressed ? "red" : "blue"}
)}
>
<Text>Click Me!</Text>
</Pressable>
visible
: 모달의 오픈상태를 관리 / boolean
animationType
: 모달 오픈/클로즈 시 애니메이션 설정 / none | fade | slide
const [modalVisible, setModalVisible] = useState(false)
<Modal
visible={modalVisible}
animationType="slide"
>
...
</Modal>
키보드가 열려도 해당 컴포넌트 내부의 요소에 액세스 할 수 있음.
가로모드 등 화면의 높이가 낮을 때, 화면이 가상키보드에 가리는 경우 활용 할 수 있음(IOS)
props
behavior
: 키보드에 반응하는 방식을 지정height
,position
, padding
<KeyboardAvoidingView behavior={'positon'}>
...
</KeyboardAvoidingView>
<View>
<ActivityIndicator size="large" color="#fff" />
</View>