gpt4 book ai didi

reactjs - HandleReset 在 formik 中使用 react 时抛出错误

转载 作者:行者123 更新时间:2023-12-04 15:33:10 25 4
gpt4 key购买 nike

我有一个表单,每次单击提交按钮时,它都会抛出此错误:“无法读取未定义的属性‘handleReset’”。这是我的代码。

import React, { Component } from "react";
import { withFormik, Form, Field, ErrorMessage } from "formik";
import * as Yup from "yup";

class MyForm extends Component {
state = {
myDetails: [
{ name: "name", type: "text", placeholder: "enter your name" },
{ name: "email", type: "email", placeholder: "enter your email" },
{
name: "password",
type: "password",
placeholder: "enter your password"
},
{ name: "dept", type: "text", placeholder: "enter your department" }
]
};
render() {
return (
<React.Fragment>
<Form>
{this.state.myDetails.map((detail, i) => (
<div key={i}>
<Field
name={detail.name}
type={detail.type}
placeholder={detail.placeholder}
/>
<ErrorMessage name={detail.name} />
</div>
))}
<button type="submit">Submit form</button>
</Form>
</React.Fragment>
);
}
}
const myFormik = withFormik({
mapPropsToValues({ name, email, password }) {
return {
name: "",
email: "",
password: ""
};
},
validationSchema: Yup.object().shape({
name: Yup.string().required(),
email: Yup.string()
.email()
.required(),
password: Yup.string().required()
}),
handleSubmit: values => {
console.log(values);
}
})(MyForm);
export default MyForm;

我该如何解决。我一直在为此苦苦挣扎,但找不到答案。

最佳答案

我已经看到了错误,我应该导出包装器“MyFormik”,但我导出了“MyForm”。

关于reactjs - HandleReset 在 formik 中使用 react 时抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60698900/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com