๐ https://reactnative.dev/docs/components-and-apis
- View: UI ๊ตฌ์ถ์ ์ํ ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ ๊ตฌ์ฑ ์์
- Text: ํ ์คํธ๋ฅผ ํ์ํ๊ธฐ์ํ ๊ตฌ์ฑ ์์
- Image: ์ด๋ฏธ์ง๋ฅผ ํ์ํ๊ธฐ์ํ ๊ตฌ์ฑ ์์
- TextInput: ํค๋ณด๋๋ฅผ ํตํด ์ฑ์ ํ ์คํธ๋ฅผ ์ ๋ ฅํ๊ธฐ ์ํ ๊ตฌ์ฑ ์์
- ScrollView: ์ฌ๋ฌ ๊ตฌ์ฑ ์์ ๋ฐ ๋ณด๊ธฐ๋ฅผ ํธ์คํ ํ ์ ์๋ ์คํฌ๋กค ์ปจํ ์ด๋
- StyleSheet: CSS ์คํ์ผ ์ํธ์ ์ ์ฌํ ์ถ์ํ ๊ณ์ธต์ ์ ๊ณต
- Button: ๋ชจ๋ ํ๋ซํผ์์ ๋ ๋๋ง๋์ด์ผ ํ๋ ํฐ์น๋ฅผ ์ฒ๋ฆฌํ๊ธฐ ์ํ ๊ธฐ๋ณธ ๋ฒํผ ๊ตฌ์ฑ ์์
- Switch: Boolean ์ ๋ ฅ์ ๋ ๋๋งํ๋ค
- FlatList: ์คํฌ๋กค ๊ฐ๋ฅํ ๋ชฉ๋ก์ ๋ ๋๋งํ๋ ๊ตฌ์ฑ ์์
- SectionList: FlatList์ ์ ์ฌํ์ง๋ง Section ๋ชฉ๋ก์ ์ฌ์ฉ๋๋ค.
- BackHandler: ๋ค๋ก ํ์์ ์ํ ํ๋์จ์ด ๋ฒํผ ๋๋ฆ์ ๊ฐ์ง
- DrawerLayoutAndroid: DrawerLayoutAndroid์์ ๋ ๋๋งํ๋ค.
- PermissionsAndroid: Android M์ ๋์ ๋ ๊ถํ ๋ชจ๋ธ์ ๋ํ ์์ธ์ค๋ฅผ ์ ๊ณต
- ToastAndroid: Android Toast ์๋ฆผ์ ๋ง๋ ๋ค.
- ActionsSheetIOS: iOS ์์ ์ํธ๋ฅผ ํ์ํ๊ฑฐ๋ ์ํธ๋ฅผ ๊ณต์ ํ๋ API
- ActivityIndicator: ์ํ ๋ก๋ฉ ํ์๊ธฐ๋ฅผ ํ์ํ๋ค.
- Alert: ์ง์ ๋ ์ ๋ชฉ๊ณผ ๋ฉ์์ง๊ฐ ์๋ ๊ฒฝ๊ณ ๋ํ ์์๋ฅผ ์คํํ๋ค.
- Animated: ์ ๋๋ฉ์ด์ ์ ๋ง๋ค๊ธฐ ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๋ค.
- Dimensions: ์ฅ์น ํฌ๊ธฐ๋ฅผ ๊ฐ์ ธ์ค๊ธฐ ์ํ ์ธํฐํ์ด์ค๋ฅผ ์ ๊ณตํ๋ค.
- KeyboardAvoidingView: ๊ฐ์ ํค๋ณด๋์์ ์๋์ผ๋ก ๋ฒ์ด๋๋ ๋ณด๊ธฐ๋ฅผ ์ ๊ณตํ๋ค.
- Linking: ์์ ๋ฐ ๋ฐ์ ์ฑ ๋งํฌ์ ์ํธ ์์ฉํ ์ ์๋ ์ธํฐํ์ด์ค๋ฅผ ์ ๊ณตํ๋ค.
- Modal: Modal ์ฝํ ์ธ ๋ฅผ ํ์ํ๋ ๊ฐ๋จํ ๋ฐฉ๋ฒ์ ์ ๊ณตํ๋ค.
- PixelRatio: ์ฅ์น ํฝ์ ๋ฐ๋์ ๋ํ ์ก์ธ์ค๋ฅผ ์ ๊ณตํ๋ค.
- RefreshControl: ScrollView pull, refresh ๊ธฐ๋ฅ์ ์ถ๊ฐํ๋๋ฐ ์ฌ์ฉ๋๋ค.
- StatusBar: ์ฑ ์ํ ํ์ ์ค์ ์ ์ดํ๋ ๊ตฌ์ฑ์์์ด๋ค.
- props๋ฅผ ์ ๋ฌ ํ ๋, ์๋ชป๋ ํ์ ์ ์ ๋ฌํ์ ๋ ๊ฒฝ๊ณ ๋ฉ์์ง๋ฅผ ๋ณด๋ด์ฃผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
MyButton.propTypes = {
//title์ string์ด์ฌ์ผ ํ๋ฉฐ, ํ์๋ก ์
๋ ฅํด์ผ ํ๋ค.
//onPress๋ผ๋ ํจ์๊ฐ ํ์์ ์ผ๋ก ์
๋ ฅํด์ผ ํ๋ค.
title: PropTypes.string.isRequired,
onPress: PropTypes.func.isRequired,
};
- JSX๋ ์ฆ์ ์คํ ํจ์๋ก ์์ฑํด์ผ์ง if๋ฌธ์ ์ฌ์ฉํ ์ ์๋ค.
<Text>
{(() => {
if(name === 'Minjae') return 'My name is Minjae';
else return 'My First React Native';
})()}
</Text>
- onPressIn: ํฐ์น๊ฐ ์์๋ ๋ ํธ์ถ
- onPressOut: ํฐ์น๊ฐ ํด์ฒด๋ ๋ ํธ์ถ
- onPress: ํฐ์น๊ฐ ํด์ฒด ๋ ๋ onPressOut ์ดํ ํธ์ถ
- onLongPress: ํฐ์น๊ฐ ์ผ์ ์๊ฐ ์ด์ ์ง์๋๋ฉด ํธ์ถ
<TouchableOpacity
onPressIn={_onPressIn}
onLongPress={_onLongPress}
onPressOut={_onPressOut}
onPress={_onPress}
//3์ด๋์ ํด๋ฆญํด์ผ onLongPress๊ฐ ํธ์ถ
delayLongPress={3000}
>
- ๋ณํ๋ฅผ ๊ฐ์งํ๋ change ์ด๋ฒคํธ๋ TextInput ์ปดํฌ๋ํธ์์ ๋ง์ด ์ฌ์ฉ๋๋ค.
- event๊ฐ์ฒด์ nativeEvent.text ๋ฅผ ์ฌ์ฉํด์ text ๊ฐ์ ์ ๋ฌํ ์ ์๋ค.
- onChangeText: ์ปดํฌ๋ํธ์ ํ ์คํธ๊ฐ ๋ณ๊ฒฝ๋ ๋, ๋ณ๊ฒฝ ๋ ํ ์คํธ์ ๋ฌธ์์ด๋ง ์ธ์๋ก ์ ๋ฌํ๋ฉฐ ํธ์ถ๋๋ค.
const _onChange = e => {
setText(e.nativeEvent.text);
}
const _onChangeText = text => {
setText(text);
}
<TextInput
style={{borderWidth: 1, padding: 10, fontSize: 20}}
placeholder="Enter a Text..."
// onChange={_onChange}
onChangeText={_onChangeText}
/>
- ๊ธฐ์กด์ TouchableOpacity ์ปดํฌ๋ํธ๋ฅผ ๋์ฒดํ๋ Pressable ์ปดํฌ๋ํธ.
- HitReact: ๋ฒํผ ๋ชจ์๋ณด๋ค ์ฝ๊ฐ ๋จ์ด์ง ๋ถ๋ถ๊น์ง ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ ์ ์๊ฒ ํด์ฃผ๋ pressable์ ๊ธฐ๋ฅ
- PressRect: ๋ฒํผ์ ๋๋ฅธ ์ํ์์ ์ผ๋ง๋ ๋ฉ์ด์ ธ์ผ ์ ํจ ๋ฒ์์์ ๋ฒ์ด๋ฌ๋ค๊ณ ํ๋จ ํ ์ ์๊ฒ ํ๋ ๊ธฐ๋ฅ
import { Text, Pressable } from 'react-native';
<Pressable
pressRetentionOffset={{bottom:50, left: 50, right: 50, top: 50,}}
hitSlop={50}
>
<Text style={{padding:10, fontSize:30}}>{props.title}</Text>
</Pressable>
- ํด๋์ค ์คํ์ผ๋ง์ ์ปดํฌ๋ํธ์ ํ๊ทธ์ ์ง์ ์ ๋ ฅํ๋ ๋ฐฉ์์ด ์๋, ์คํ์ผ์ํธ์ ์ ์๋ ์คํ์ผ์ ์ฌ์ฉํ๋ ๋ฐฉ์์ด๋ค.
//์ฌ๋ฌ๊ฐ์ ์คํ์ผ ์ ์ฉ1, ๋ค์ ์ค๋ ์คํ์ผ์ด ์์ ์๋ ์คํ์ผ์ ๋ฎ์
<Text style={[styles.text, styles.error]}>Class Style-Error</Text>
//์ฌ๋ฌ๊ฐ์ ์คํ์ผ ์ ์ฉ2
<Text style={[styles.text, {color: 'green'}]}>Class Style-Text</Text>
- Platform ๋ชจ๋์ ํตํด์ ๊ฐ ํ๋ซํผ๋ง๋ค ๋ค๋ฅธ ์ฝ๋๊ฐ ์ ์ฉ๋๋๋ก ์ฝ๋๋ฅผ ์์ฑํ ์ ์๋ค.
- iOS์์ shadow๋ shadowColor(์), shadowOffset(๊ฑฐ๋ฆฌ), shadowOpacity(๋ถํฌ๋ช ๋), shadowRadius(ํ๋ฆ ๋ฐ๊ฒฝ) ๋ฑ์ ์คํ์ผ ์์ฑ์ด ์กด์ฌํฉ๋๋ค.
- Android์์๋ elevation ์ด๋ผ๋ ์์ฑ์ด ์์ต๋๋ค.
const styles = StyleSheet.create({
shadow: {
...Platform.select({
ios: {
shadowColor: '#000',
shadowOffset: {
width: 10,
height: 10,
},
shadowOpacity: 0.5,
shadowRadius: 10,
},
android: {
elevation: 20,
}
}),
},
});
๐ https://styled-components.com/
- ๋ฆฌ์กํธ ๋ค์ดํฐ๋ธ์ ์คํ์ผ์์ ์น ํ๋ก๊ทธ๋๋ฐ๊ณผ์ ์ฐจ์ด๋ก ์ดํ ๋ถํธํจ์ ํด์์์ผ ์ฃผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
- styled.[์ปดํฌ๋ํธ ์ด๋ฆ] ํํ ๋ค์ ๋ฐฑํฑ(`)์ ์ฌ์ฉํ์ฌ ๋ง๋ ๋ฌธ์์ด์ ๋ถ์ด๊ณ ๊ทธ ์์ ์คํ์ผ์ ์ง์ ํ๋ฉด ๋๋ค. ์ด๋ฌํ ๋ฌธ๋ฒ์ ํ๊ทธ๋ ํ ํ๋ฆฟ ๋ฆฌํฐ๋ด(Tagged Template Literals) ์ด๋ผ๊ณ ๋ถ๋ฅธ๋ค.
//์คํ์ผ ์ ์ฉ ์์
const Container = styled.View`
flex: 1;
background-color: #fff;
align-items: center;
justify-content: center;
`;
- Styled-components ์์๋ ์คํ์ผ์ ์์ฑํ๋ ๋ฒกํฐ ์์์ props์ ์ ๊ทผํ ์ ์๋ค๋ ์ฅ์ ์ด ์์ต๋๋ค. ์ด ์ฅ์ ์ ์ด์ฉํด ์คํ์ผ์ ์์ฑํ๋ ๊ณณ์์ ์กฐ๊ฑด์ ๋ฐ๋ผ ์คํ์ผ์ ๋ณ๊ฒฝํ ์ ์์ต๋๋ค.
const ButtonContainer = styled.TouchableOpacity`
background-color: ${props => props.title === 'Minjae' ? '#3498db' : '#9b59b6'};
border-radius: 15px;
padding: 15px 40px;
margin: 10px 0px;
justify-content: center;
`;
const Button = ({ title }) => {
return (
<ButtonContainer title={title}>
<Title>{title}</Title>
</ButtonContainer>
);
};
- attrs๋ Styled-components์์ ์์ฑ์ ์ค์ ํ ๋ ์ฌ์ฉํ๋ค.
//components/Input.js
const StyledInput = styled.TextInput.attrs(props => ({
placeholder: 'Enter a text...',
placeholderTextColor: props.borderColor,
}))`
(...)
border-color: ${props => props.borderColor};
`;
const Input = ({borderColor}) => {
return (
<StyledInput borderColor={borderColor} />
)
}
//App.js
<Input borderColor='#3498db'></Input>
<Input borderColor='#9b59b6'></Input>