
React Native์์ ํผ์ ๋ค๋ฃฐ ๋
๋จ์ํ <TextInput />๋ง ์ฐ๋ฉด ๋ ๊ฒ ๊ฐ์ง๋ง,
์ค์ ๋ก๋ ์๊ฐ๋ณด๋ค ์ ๊ฒฝ ์จ์ผ ํ ํฌ์ธํธ๊ฐ ๋ง๋ค.
์ด๋ฒ ๊ธ์ ๊ธฐ๋ณธ์ ์ธ TextInput ์ฌ์ฉ๋ฒ๋ถํฐ
ํค๋ณด๋ ๋์, ์ํ ๊ด๋ฆฌ, ์ ํจ์ฑ ๊ฒ์ฆ๊น์ง ํ ๋ฒ์ ์ ๋ฆฌํ ๊ธ์ด๋ค.
import { TextInput, View } from 'react-native';
import { useState } from 'react';
export default function Login() {
const [email, setEmail] = useState('');
return (
<View>
<TextInput
value={email}
onChangeText={setEmail}
placeholder="์ด๋ฉ์ผ์ ์
๋ ฅํ์ธ์"
keyboardType="email-address"
autoCapitalize="none"
style={{
borderWidth: 1,
padding: 12,
borderRadius: 8,
}}
/>
</View>
);
}
| ์์ฑ | ์ค๋ช |
|---|---|
placeholder | ์ ๋ ฅ ์ ์๋ด ๋ฌธ๊ตฌ |
keyboardType | ํค๋ณด๋ ํ์
(default, email-address, numeric) |
secureTextEntry | ๋น๋ฐ๋ฒํธ ์ ๋ ฅ ์ฒ๋ฆฌ |
autoCapitalize | ์๋ ๋๋ฌธ์ ์ค์ (none, sentences) |
returnKeyType | ํค๋ณด๋์ Enter ํค ์คํ์ผ |
onSubmitEditing | Enter ๋๋ ์ ๋ ์คํ๋๋ ํจ์ |
import { KeyboardAvoidingView, Platform } from 'react-native';
<KeyboardAvoidingView
behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
style={{ flex: 1 }}
>
{/* ์
๋ ฅ์ฐฝ๋ค */}
</KeyboardAvoidingView>
KeyboardAvoidingView๋ ํค๋ณด๋๊ฐ ์ฌ๋ผ์ฌ ๋ ๋ทฐ์ ์์น๋ฅผ ์กฐ์ Platform.OS ๋ถ๊ธฐ ์ฒ๋ฆฌ ํ์const secondInputRef = useRef(null);
<TextInput
onSubmitEditing={() => secondInputRef.current?.focus()}
/>
<TextInput
ref={secondInputRef}
/>
ref๋ฅผ ์ฌ์ฉํด ์ ์ดnpm install formik yup
import { Formik } from 'formik';
import * as Yup from 'yup';
const schema = Yup.object().shape({
email: Yup.string().email().required('์ด๋ฉ์ผ์ ํ์์
๋๋ค'),
});
<Formik
initialValues={{ email: '' }}
validationSchema={schema}
onSubmit={(values) => console.log(values)}
>
{({ handleChange, handleSubmit, values, errors }) => (
<>
<TextInput
value={values.email}
onChangeText={handleChange('email')}
placeholder="์ด๋ฉ์ผ"
/>
<Text>{errors.email}</Text>
<Button title="์ ์ถ" onPress={handleSubmit} />
</>
)}
</Formik>
โ
์๋ ์ ํจ์ฑ ๊ฒ์ฌ
โ
๊ฐ๋จํ ์๋ฌ ์ฒ๋ฆฌ
โ
handleChange, handleSubmit ๋ฑ ์ ํธ ์ ๊ณต
์ฒ์์ TextInput๋ง ๋ฃ์ผ๋ฉด ๋ค ๋ ์ค ์์๋๋ฐ,
์ค์ ์ฑ์์ ํค๋ณด๋ ๊ฐ๋ฆผ ๋ฌธ์ , ์ํฐ ์ด๋, ์
๋ ฅ ๊ฐ ์ ํจ์ฑ ๋ฑ
์๊ณ ๊ท์ฐฎ์ ๋ฌธ์ ๋ค์ด UX ์ ์ฒด๋ฅผ ๊ฒฐ์ ํ๋ค๋ ๊ฑธ ๋๊ผ๋ค.
์ง๊ธ์ KeyboardAvoidingView + Formik + Yup ์กฐํฉ์ผ๋ก
ํผ ์
๋ ฅ์ ํ์คํํด์ ๋ง๋ค๊ณ ์๋ค.
๐งพ "๋ชจ๋ฐ์ผ UX๋ ํผ ์ ๋ ฅ์์ ๊ฐ๋ฆฐ๋ค. ์์ง๋ง ์ค์ํ๋ค."