Created
February 27, 2020 09:25
-
-
Save wisaruthk/78d9972eb17b25b6dff69eace0353b1c to your computer and use it in GitHub Desktop.
react-native example formik and yup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Formik } from 'formik'; | |
import * as Yup from 'yup'; | |
class SignInScreen extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
username: null, | |
password: null, | |
rememberMe: false, | |
statusMessage: null, | |
loading: false | |
}; | |
} | |
static navigationOptions = { | |
title: 'Please sign in', | |
}; | |
render() { | |
return ( | |
<View style={styles.container}> | |
<Formik enableReinitialize | |
initialValues={{username:this.state.username,email:''}} | |
validationSchema={SignupSchema} | |
onSubmit={values=>console.log(values)}> | |
{({handleChange, handleBlur, handleSubmit, values, errors, touched})=>( | |
<Card> | |
<Input | |
label="Username" | |
onChangeText={handleChange('username')} | |
onBlur={handleBlur('username')} | |
value={values.username} | |
/> | |
{errors.username && touched.username ? (<Text>{errors.username}</Text>): null} | |
<Input | |
label="Email" | |
onChangeText={handleChange('email')} | |
onBlur={handleBlur('email')} | |
value={values.email} | |
/> | |
{errors.email && touched.email ? (<Text>{errors.email}</Text>): null} | |
<Button onPress={handleSubmit} style={{margin:10}} title="Submit"/> | |
</Card> | |
)} | |
</Formik> | |
</View> | |
); | |
} | |
} | |
const SignupSchema = Yup.object().shape({ | |
username: Yup.string() | |
.min(2,'Too Short!') | |
.max(50, 'Too Long!') | |
.required('Required'), | |
email: Yup.string() | |
.email('Invalid email') | |
.required('Required') | |
}) | |
export default SignInScreen; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment