简体   繁体   English

如何从 StackNavigator 提交 formik?

[英]How to submit formik from StackNavigator?

I want to have Button which submit Formik Form inside of StackNavigator.我想要在 StackNavigator 中提交 Formik Form 的 Button。 Is there any good option to do it without losing performance and have my yup validation working?有没有什么好的选择可以在不损失性能的情况下让我的 yup 验证正常工作? I managed to to it with passing state but app was running really slow and yup sadly stopped working well.我通过 state 设法做到了,但应用程序运行速度非常慢,是的,遗憾的是,它停止运行良好。 Here is code of my componenet.这是我的组件的代码。 I'll be glad if someone solved such an issue before.如果有人以前解决过这样的问题,我会很高兴。 :) :)

import { Formik } from 'formik';
import React, { useState, useEffect } from 'react';
import { StatusBar, StyleSheet, TouchableOpacity, View } from 'react-native';
import * as Yup from 'yup';
import Button from '../../_layout/Button';
import Input from '../../_layout/Input';
import { box, color, font, typography, hitSlop } from '../../../assets/global';
import { CheckIcon } from '../../../assets/icons';
import { withNavigation } from 'react-navigation';
import PropTypes from 'prop-types';

const Note = ({ navigation }) => {
    const { textAreaContainer, textArea } = styles;

    const onSubmit = (values, actions) => {
        console.log(values);
        navigation.goBack();
    };

    const validationSchema = Yup.object().shape({
        header: Yup.string()
            .required('')
            .min(3, ''),
        text: Yup.string()
            .required('')
            .min(5, ''),
    });

    return (
        <Formik
            initialValues={{ header: '', text: '' }}
            onSubmit={onSubmit}
            validationSchema={validationSchema}
            validateOnChange={true}
        >
            {({
                handleChange,
                handleSubmit,
                handleBlur,
                values,
                errors,
                isValid,
                touched,
            }) => (
                <View style={textAreaContainer}>
                    <StatusBar barStyle="light-content" />
                    <Input
                        name="header"
                        onChangeText={handleChange('header')}
                        placeholder="Wprowadz tytuł"
                        type="outlined"
                        onBlur={handleBlur('header')}
                        containerStyle={{ marginBottom: 0 }}
                        value={values.header}
                        error={errors.header}
                        touched={touched.header}
                        displayTitle={false}
                    />
                    <Input
                        onChangeText={handleChange('text')}
                        placeholder="Wprowadź tekst"
                        type="outlined"
                        onBlur={handleBlur('text')}
                        style={textArea}
                        numberOfLines={10}
                        multiline={true}
                        value={values.text}
                        error={errors.text}
                        touched={touched.text}
                        displayTitle={false}
                    />

                    <Button title="test" onPress={handleSubmit} />
                </View>
            )}
        </Formik>
    );
};

Note.propTypes = {
    navigation: PropTypes.object.isRequired,
};

Note.navigationOptions = ({ navigation }) => ({
    headerTitle: 'Notatka',
    headerRight: (
        <TouchableOpacity
            onPress={() => {
                console.log('SUBMIT FORMIK HERE!!!!!!!!!!!!!!!!!!!');
            }}
            hitSlop={hitSlop()}
        >
            <CheckIcon
                style={{ marginRight: box }}
                width={25}
                height={25}
                stroke={color.white}
            />
        </TouchableOpacity>
    ),
});

export default withNavigation(Note);

const styles = StyleSheet.create({
    textAreaContainer: {
        padding: box,
    },
    textArea: {
        padding: box,
        marginBottom: 10,
        height: 150,
        textAlignVertical: 'top',
        ...typography(color.black, font.S),
        borderColor: color.lightGray,
        borderWidth: 1,
        borderRadius: 5,
    },
});

You can call functions from react-navigation header buttons as followed:您可以从 react-navigation header 按钮调用函数,如下所示:

  1. Create an effect that uses setParams from navigation.创建一个使用导航中的setParams的效果。 Define a function eg onPress in there you can define what every you want to run on button press.定义一个 function 例如onPress在那里你可以定义你想在按钮按下时运行的每一个。
 useEffect(() => {
    setParams({
      onPress: () => {
        validateForm(); // example
        submitForm(); // example
      },
    });
  }, []);
  1. You need to use the onPress function within your header您需要在 header 中使用 onPress function
Note.navigationOptions = ({ navigation }) => ({
    headerTitle: 'Notatka',
    headerRight: (
        <TouchableOpacity
            onPress={() => {
              const onPress = navigation.getParam("onPress"); // get param onPress
              if (onPress) onPress();
            }}
            hitSlop={hitSlop()}
        >
            <CheckIcon
                style={{ marginRight: box }}
                width={25}
                height={25}
                stroke={color.white}
            />
        </TouchableOpacity>
    ),
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM