如何使用 Formik 在 headerRight 中触发一个函数?

How can I fire a function in headerRight using Formik?

我是 react-native 和 formik 的新手,我遇到了这个我正在尝试解决的问题。

如何使用 Formik 在 headerRight 中触发一个函数?我有 updateCorporation 函数,它会触发 api,而 formik 会触发这个函数,在我按下更新按钮后,但结果是不确定的 我不明白为什么会这样。

File_1.js

const CorporationContainer = (props) => {
    const {
    navigation,
} = props;

const updateCorporation = (values) => {
    // do patch stuff with values
    // but its undefined
};

useEffect(() => {
    navigation.setParams({ updateCorporation: updateCorporation.bind() });
}, []);

return (
    <Corporation
        updateCorporation={updateCorporation} />
    );
};

CorporationContainer.navigationOptions = ({ navigation }) => ({
    headerRight: (
        <EditBtn
        onPress={() => navigation.state.params.updateCorporation()}
        >
        <EditText>Update</EditText>
        </EditBtn>
    ),
});

export default CorporationContainer;

File_2.js

const Corporation = (props) => {
    const {
        updateCorporation,
    } = props;

    const emailField = useRef(null);

    const validationSchema = yup.object().shape({
        email: yup.string()
        .ensure()
        .email('Email must be valid')
        .required('Email'),
    });

    return (
        <Formik
            initialValues={{
                email,
            }}
            onSubmit={values => updateCorporation(values)}
            validateOnBlur={false}
            validateOnChange={false}
            validationSchema={validationSchema}
            >
        {(formProps) => {
            const {
            errors,
            setFieldValue,
            values,
            } = formProps;
            return (
                <Container>
                    <Input
                        name="email"
                        placeholder="Email Corporation"
                        textContentType="emailAddress"
                        keyboardType="email-address"
                        returnKeyType="done"
                        autoCapitalize="none"
                        autoCorrect={false}
                        ref={emailField}
                        value={values.email}
                        onChangeText={setFieldValue}
                        editable={!email}
                        error={errors.email}}
                    />
                </Container>
            );
        }}
        </Formik>
    );
};

export default Corporation;

Formik 作者在这里...

还没有尝试过这个,我知道导航绑定是如何工作的,但你想将 Formik 的 submitForm() 属性绑定到导航而不是 updateCorporation 函数。但是,您需要在有权访问 Formik props/context 的地方执行此操作(即作为 <Formik> 的子级)。

import React from 'react'
import { connect } from 'formik'

const updateCorporation = (values) => {
    // do patch stuff with values
    // but its undefined
};

const BindSubmit = connect(({ formik, navigation }) => {
  useEffect(() => {
    navigation.setParams({ updateCorporation: submitForm.bind() });
   }, []);
  return null
})


// ... and then just render it somewhere under Formik

const Corporation = () => {
 return (
   <Formik>
    <BindSubmit />
    {/* ... same */}
   </Formik>
 )
}

在 File_1.js 中,我不得不使用 withForm 并删除 File_2.js 中的所有 Formik 东西并改用道具。

const CorporationContainer = (props) => {
    const {
        navigation,
        handleSubmit,
        errors,
        setFieldValue,
        values,
    } = props;

    useEffect(() => {
        navigation.setParams({ updateCorporation: handleSubmit.bind() });
    }, []);

    return (
        <ProfileProfessional
        errors={errors}
        setFieldValue={setFieldValue}
        values={values}
        />
    );
};

CorporationContainer.navigationOptions = ({ navigation }) => ({
headerRight: (
    <EditBtn
    onPress={() => navigation.state.params.updateCorporation()}
    >
    <EditText>Editar</EditText>
    </EditBtn>
),
});

export default withFormik({
    // ...
})(CorporationContainer);