mcc*_*osa 4 javascript reactjs formik
我使用带有前向引用的 formik 形式
import React from "react";
import FormikWithRef from "./FormikWithRef";
const Form = ({
formRef,
children,
initialValues,
validationSchema,
onSubmit
}) => {
return (
<FormikWithRef
validateOnChange={true}
validateOnBlur={true}
initialValues={initialValues}
validationSchema={validationSchema}
onSubmit={onSubmit}
ref={formRef}
>
{(props) => <form onSubmit={props.handleSubmit}>{children}</form>}
</FormikWithRef>
);
};
export default Form;
Run Code Online (Sandbox Code Playgroud)
import React, { forwardRef, useImperativeHandle } from "react";
import { Formik } from "formik";
function FormikWithRef(props, ref) {
let _formikProps = {};
useImperativeHandle(ref, () => _formikProps);
return (
<Formik {...props}>
{(formikProps) => {
_formikProps = formikProps;
if (typeof props.children === "function") {
return props.children(formikProps);
}
return props.children;
}}
</Formik>
);
}
export default forwardRef(FormikWithRef);
Run Code Online (Sandbox Code Playgroud)
我有一些选项卡,用于更新easy-peasy商店状态type,当我选择第二个选项卡时,我想value使用 Formik 表单更新输入值(最初来自 的商店状态),但更新initialValues特定于该组件的状态作为initialValues道具传递给Formik组件。
import React, { useState, useEffect, useRef } from "react";
import styled from "styled-components";
import { useStoreState } from "easy-peasy";
import Form from "./Form";
import MoneyBox from "./MoneyBox";
const Container = styled.div`
width: 100%;
background-color: #dfdfdf;
`;
const FieldWrapper = styled.div`
padding: 20px 12px;
`;
const TabsForm = () => {
const [initialValues, setInitialValues] = useState();
const type = useStoreState((state) => state.type);
const value = useStoreState((state) => state.value);
const formRef = useRef(null);
const onFormSubmit = async (values) => {
console.log({ values });
};
useEffect(() => {
if (value && type) {
let filterVal = { ...value };
/* here is where I update the input value to be 3000,
the initial values get updated and in the `Form.js` file,
the console log from here also reflects this update,
however, the input field does not update? */
if (type === "Two") filterVal.input = 30000;
setInitialValues(filterVal);
}
}, [value, type]);
useEffect(() => {
// check initialValues has updated
console.log({ initialValues });
}, [initialValues]);
return (
<Container>
{initialValues && type ? (
<Form
initialValues={initialValues}
onSubmit={onFormSubmit}
formRef={formRef}
>
<FieldWrapper>
<MoneyBox name="input" currencySymbol={"£"} />
</FieldWrapper>
</Form>
) : null}
</Container>
);
};
export default TabsForm;
Run Code Online (Sandbox Code Playgroud)
单击第二个选项卡时;
initialValues状态TabsForms.js更新,以便value.input= 30000;initialValues在这两个支柱Form.js和FormikWithRef.js也反映value.input=3000useField钩从forimk在MoneyBox.js组件中,field对象不具有value的30000,而不是它的任何字段值是以前,这是为什么?我创建了一个CodeSandbox来查看所有使用的组件,并创建了控制台日志来查看 Formik 确实收到了更新的值,但似乎没有应用它。
我已经坚持了几天,似乎无法找到解决方案,任何帮助将不胜感激。
Ven*_*sky 10
如果您希望输入的值在更改时更改initialValues,则需要Formik将 propenableReinitialize作为传递给组件true。
因此,您需要在代码中更改的是TabsForm.js传递给Form组件的 propenableReinitialize
<Form
enableReinitialize
initialValues={initialValues}
onSubmit={onFormSubmit}
formRef={formRef}
>
<FieldWrapper>
<MoneyBox name="input" currencySymbol={"£"} />
</FieldWrapper>
</Form>
Run Code Online (Sandbox Code Playgroud)
并在您Form.js将道具传递给Formik组件时
const Form = ({
formRef,
children,
initialValues,
validationSchema,
onSubmit,
enableReinitialize
}) => {
return (
<FormikWithRef
enableReinitialize={enableReinitialize}
validateOnChange={true}
validateOnBlur={true}
initialValues={initialValues}
validationSchema={validationSchema}
onSubmit={onSubmit}
ref={formRef}
>
{(props) => <form onSubmit={props.handleSubmit}>{children}</form>}
</FormikWithRef>
);
};
Run Code Online (Sandbox Code Playgroud)
我不太确定您的业务逻辑应该如何工作,但这里有一个包含上述更改的工作示例。
| 归档时间: |
|
| 查看次数: |
5361 次 |
| 最近记录: |