<TextInput>
即使您只是指定占位符,通常也允许您输入。但是,在 Formik 表单中使用时,我无法在表单中键入和验证任何内容。我究竟做错了什么?
我曾尝试onChangeText
与setFieldValue
和handleChange
两者。
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>
);
};
如何修复 textInput 以使打字和验证工作?
根据这个:https : //jaredpalmer.com/formik/docs/guides/react-native
我们不需要使用 name 属性。我也用过这个 onChangeText 但我仍然不能写任何东西。