Simple Sign Up
import React, { useState } from 'react';
import { View, Text, TextInput, Button, StyleSheet, Alert } from 'react-native';
import { Formik } from 'formik';
import * as Yup from 'yup';
const SignupSchema = Yup.object().shape({
name: Yup.string()
.min(2, 'Name is too short!')
.required('Name is required!'),
email: Yup.string()
.email('Invalid email')
.required('Email is required!'),
password: Yup.string()
.min(6, 'Password is too short!')
.required('Password is required!'),
});
const SignupScreen = () => {
const handleSignup = (values) => {
// You can handle the signup logic here, e.g., send data to your backend
Alert.alert('Signup Successful', `Welcome, ${values.name}!`);
};
return (
<View style={styles.container}>
<Text style={styles.header}>Sign Up</Text>
<Formik
initialValues={{ name: '', email: '', password: '' }}
validationSchema={SignupSchema}
onSubmit={handleSignup}
>
{({ handleChange, handleBlur, handleSubmit, values, errors, touched }) => (
<>
<TextInput
style={styles.input}
placeholder="Name"
onChangeText={handleChange('name')}
onBlur={handleBlur('name')}
value={values.name}
/>
{touched.name && errors.name && <Text style={styles.error}>{errors.name}</Text>}
<TextInput
style={styles.input}
placeholder="Email"
keyboardType="email-address"
onChangeText={handleChange('email')}
onBlur={handleBlur('email')}
value={values.email}
/>
{touched.email && errors.email && <Text style={styles.error}>{errors.email}</Text>}
<TextInput
style={styles.input}
placeholder="Password"
secureTextEntry
onChangeText={handleChange('password')}
onBlur={handleBlur('password')}
value={values.password}
/>
{touched.password && errors.password && <Text style={styles.error}>{errors.password}</Text>}
<Button title="Sign Up" onPress={handleSubmit} />
</>
)}
</Formik>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
padding: 16,
},
header: {
fontSize: 24,
marginBottom: 20,
},
input: {
width: '100%',
padding: 10,
marginVertical: 8,
borderWidth: 1,
borderColor: '#ccc',
borderRadius: 4,
},
error: {
color: 'red',
fontSize: 12,
},
});
export default SignupScreen;
See More