Skip to content

Instantly share code, notes, and snippets.

@singh100ful
Created May 28, 2020 23:20
Show Gist options
  • Select an option

  • Save singh100ful/bc81b8e1b906e304ffa3ba2a7b05d56f to your computer and use it in GitHub Desktop.

Select an option

Save singh100ful/bc81b8e1b906e304ffa3ba2a7b05d56f to your computer and use it in GitHub Desktop.
React Native Log In Form
import React, {Component} from 'react';
import {Formik} from 'formik';
import * as yup from 'yup';
import {
View,
TextInput,
Text,
Button
} from 'react-native';
export deafault class App extends Component {
handleSubmit = (values, {props = this.props}) => {
let data = {
email: values.email,
password: values.password,
};
return fetch(baseUrl, {
method: "post",
headers: new Headers({
Accept: "application/json",
'Content-Type': 'application/json',
Authorization: token,
}),
body: JSON.stringify(data),
})
.then((response) => response.json())
.catch((error) => console.log(error));
};
render(){
return(
<Formik
initialValues={{ email: '', password: ''}}
validationSchema={yup.object().shape({
email: yup
.string()
.email('Invalid Email')
.required('Email is Required.'),
password: yup.string()
.min(6)
.required('Password is Required'),
})}
onSubmit={this.handleSubmit}>
{(formProps) => (
<View>
<TextInput
value={formProps.values.email}
onChangeText={formProps.handleChange('email')}
onBlur={() => formProps.setFieldTouched('email')}
placeholder="Enter Email"
/>
{formProps.touched.email && formProps.errors.email && (
<Text
style={{
fontSize: 16,
color: 'red',
paddingHorizontal: 10,
}}>
{formProps.errors.email}
</Text>
)}
<TextInput
value={formProps.values.password}
onChangeText={formProps.handleChange('password')}
onBlur={() => formProps.setFieldTouched('email')}
placeholder="Password"
secureTextEntry={true}
/>
{formProps.touched.password && formProps.errors.password && (
<Text
style={{
fontSize: 16,
color: 'red',
paddingHorizontal: 10,
}}>
{formProps.errors.password}
</Text>
<Button
title="Log In"
disabled={!formProps.isValid}
onPress={formProps.handleSubmit}
/>
</View>
)}
</Formik>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment