Generally <TextInput>
allows you to type even when you're just specifying a placeholder. However, when using within the Formik form, I am unable to type and validate anything in my form. What am I doing wrong?
I have tried onChangeText
with setFieldValue
and handleChange
both.
const initialValues: FormValues = {
friendEmail: '',
};
export const Page: React.FunctionComponent<Props> = ({
toggleShowPage,
showPage,
}) => {
const validationSchema = emailValidationSchema;
const getFriendId = React.useCallback(
(data: any) => {
//console.log('Email', initialValues.friendEmail);
if (data) {
if (data.users.nodes.length == 0) {
...
} else {
addFriend(Number(data.users.nodes[0].id));
}
}
},
[addFriend],
//[friendEmail, addFriend],
);
const [loadUsers] = useUsersLazyQuery({
onCompleted: getFriendId,
onError: _onLoadUserError,
});
const handleSubmitForm = React.useCallback(
(values: FormValues, helpers: FormikHelpers<FormValues>) => {
console.log('Submitted');
loadUsers({
variables: {
where: { email: values.friendEmail },
},
});
//setFriendEmail('');
values.friendEmail = '';
},
[loadUsers],
//[loadUsers, friendEmail]
);
return (
<Modal
visible={showPage}
animationType="slide"
transparent={true}>
<SafeAreaView>
<View>
<View>
<View>
<Formik
initialValues={initialValues}
onSubmit={handleSubmitForm}
validationSchema={validationSchema}>
{({
handleChange,
setFieldValue,
setFieldTouched,
handleBlur,
handleSubmit,
isSubmitting,
values,
}) => {
const setEmail = (friendEmail: string) => {
setFieldValue('friendEmail', friendEmail)
setFieldTouched('friendEmail', true, false);
}
return(
<Form>
<View>
<View>
<Item>
<TextInput
placeholder="Email"
onChangeText={setEmail}
//onChangeText={handleChange}
//onChangeText={handleChange('friendEmail')}
//onChangeText={e => console.log('Workinggg')}
//onBlur={handleBlur('friendEmail')}
//value={values.friendEmail}
autoCapitalize="none"
/>
</Item>
</View>
<View>
<Button
onPress={handleSubmit}>
<Text>
Add{' '}
</Text>
</Button>
</View>
</View>
</Form>
)}}
</Formik>
</View>
</View>
</View>
</SafeAreaView>
</Modal>
);
};
How can I fix the textInput to make the typing and validation work?
According to this: https://jaredpalmer.com/formik/docs/guides/react-native
We don't need to use the name property. I have used this onChangeText too but I still can't write anything.
onChangeText={setEmail}
prop toTextInput
passonChange={handleChange}
– Defalcate