反応(とネイティブ)を提出し、フォームの外からFormekとの検証
9772 ワード
フォームの外部からFormIkからSubmitイベントを呼び出す必要があるとしましょう.The
FORMIKからフォームのデータをフォームrefに保存すると、フォームからのすべての重要な状態にアクセスできます
Formekの検証についての詳細はhttps://formik.org/docs/guides/validation
useRef
フックはここで役立ちます.import React, { useState, useRef } from 'react';
const YourComponent = () => {
const formRef = useRef();
const [input, setInput] = useState({});
const [step, setStep] = useState(1);
const saveInput = () => {
if (formRef.current) {
formRef.current.handleSubmit();
if (formRef.current.isValid) {
setStep(2);
}
}
};
const ValidationSchema = Yup.object().shape({
name: Yup.string().min(2, 'Too Short!').max(50, 'Too Long!').required('Required'),
});
return (
<View>
<Formik
initialValues={{
name: '',
description: '',
points: '',
redemptionsTotal: '',
redemptionsPerUser: '',
}}
validationSchema={QrCodeSchema}
validateOnChange={true}
validateOnBlur={false}
validateOnMount
onSubmit={(values) => setInput(values)}
innerRef={formRef}
>
{({ handleChange, handleBlur, values, touched, errors }) => {
return (
<FormInput
allowFontScaling={false}
onChangeText={handleChange('name')}
onBlur={handleBlur('name')}
value={values.name}
error={errors.name}
touched={touched.name}
name="name"
placeholder="TITLE"
returnKeyType="next"
blurOnSubmit={false}
style={styles.inputStyle}
/>
);
}}
</Formik>
<YourActionsComponent>
<Button name="submit" title={'Define Dates'} onClick={() => saveInput()} style={styles.stepButton} />
</YourActionsComponent>
</View>
);
};
この小さな例では、ユーザーがすべての段階でいくつかの入力を塗りつぶすウィザードのようなモーダルを持っている場所で、フォームがマウントされたときに、無効な入力があるかどうかをチェックします.FORMIKからフォームのデータをフォームrefに保存すると、フォームからのすべての重要な状態にアクセスできます
console.log(formRef.current)
を参照してください.Formekの検証についての詳細はhttps://formik.org/docs/guides/validation
Reference
この問題について(反応(とネイティブ)を提出し、フォームの外からFormekとの検証), 我々は、より多くの情報をここで見つけました https://dev.to/morenomdz/react-native-submit-and-validate-with-formik-from-outside-the-form-2ogaテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol