15.6.4 AuthForm 컴포넌트 만들기

    AuthForm이라는 컴포넌트를 만들어서 이를 통해 회원가입 또는 로그인 기능을 구현하겠습니다. 이 컴포넌트는 isRegister Props를 통해 컴포넌트에서 회원가입 UI 또는 로그인 UI를 보여줄지 결정합니다.

    components/AuthForm.tsx

    import React, {useState} from 'react';
    import {
      View,
      StyleSheet,
      Pressable,
      Platform,
      Text,
      TextInput,
      KeyboardAvoidingView,
    } from 'react-native';
    
    export interface AuthFormProps {
      isRegister?: boolean;
    }
    
    function AuthForm({isRegister}: AuthFormProps) {
      const [email, setEmail] = useState('');
      const [username, setUsername] = useState('');
      const [identifier, setIdentifier] = useState('');
      const [password, setPassword] = useState('');
      return (
        <KeyboardAvoidingView
            style={styles.block}
            behavior={Platform.select({ios: 'padding'})}>
          <View style={styles.block}>
            <View>
              {isRegister ? (
                <>
                  <TextInput
                    style={styles.input}
                    placeholder="이메일"
                    value={email}
                    onChangeText={setEmail}
                    autoCapitalize="none"
                    keyboardType="email-address"
                  />
                  <TextInput
                    style={styles.input}
                    placeholder="계정명"
                    value={username}
                    onChangeText={setUsername}
                    autoCapitalize="none"
                  />
                </>
              ) : (
                <TextInput
                  style={styles.input}
                  placeholder="이메일 또는 계정명"
                  value={identifier}
                  onChangeText={setIdentifier}
                  autoCapitalize="none"
                />
              )}
              <TextInput
                style={styles.input}
                placeholder="비밀번호"
                secureTextEntry
                value={password}
                onChangeText={setPassword}
              />
              <Pressable
                style={({pressed}) => [
                  styles.submit,
                  Platform.OS === 'ios' && pressed && styles.submitPressed,
                ]}
                android_ripple={{color: '#42a5f5'}}>
                <Text style={styles.submitText}>
                  {isRegister ? '회원가입' : '로그인'}
                </Text>
              </Pressable>
            </View>
          </View>
        </KeyboardAvoidingView>
      );
    }
    
    const styles = StyleSheet.create({
      block: {
        flex: 1,
        justifyContent: 'center',
        paddingHorizontal: 12,
      },
      input: {
        backgroundColor: 'white',
        padding: 8,
        borderColor: '#dddddd',
        borderWidth: 1,
        marginBottom: 8,
      },
      submit: {
        marginTop: 24,
        backgroundColor: '#2196f3',
        height: 56,
        borderRadius: 4,
        alignItems: 'center',
        justifyContent: 'center',
      },
      submitPressed: {
        opacity: 0.75,
      },
      submitText: {
        fontSize: 16,
        color: 'white',
        fontWeight: 'bold',
      },
    });
    
    export default AuthForm;
    신간 소식 구독하기
    뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.