Created
October 4, 2020 01:52
-
-
Save MorenoMdz/c6f0882a9d1ca1265b71dea5a15ea4f5 to your computer and use it in GitHub Desktop.
Submit Form from outside Formik RN
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 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> | |
| ); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment