@react-native-school/expo-typescript-template
Version:
A simple Expo template with navigation, testing, and linting setup.
44 lines (37 loc) • 939 B
text/typescript
import React, { Dispatch, SetStateAction } from 'react';
import { Alert } from 'react-native';
type ErrorType = {
email?: string;
password?: string;
};
export const useLogin = () => {
const [email, setEmail] = React.useState('');
const [password, setPassword] = React.useState('');
const [errors, setErrors]: [
ErrorType,
Dispatch<SetStateAction<{}>>,
] = React.useState({});
const submit = () => {
const nextErrors: ErrorType = {};
if (email.length === 0) {
nextErrors.email = 'This field is required.';
}
if (password.length === 0) {
nextErrors.password = 'This field is required.';
}
setErrors(nextErrors);
if (Object.keys(nextErrors).length > 0) {
return null;
}
Alert.alert('Success!', `Email: ${email} \n Password: ${password}`);
return null;
};
return {
submit,
errors,
email,
setEmail,
password,
setPassword,
};
};