Ale*_*x B 2 default object reactjs formik
使用 时Formik,我的输入会显示一个[object Object]值,而不是以标准方式表现。
import React from "react";
import ReactDOM from "react-dom";
import { Formik, Field, Form, ErrorMessage } from "formik";
import * as Yup from "yup";
import "./styles.css";
const SignupForm = () => {
return (
<Formik
initialValues={{ email: "" }}
validationSchema={Yup.object({
email: Yup.string()
.email("Mauvais e-mail")
.required("Champ requis")
})}
onSubmit={values => {
alert(JSON.stringify(values, null, 2));
}}
>
<Form>
<label htmlFor="email">Email</label>
<ErrorMessage name="email" />
<br />
<Field name="email" type="email" />
<button type="submit">OK</button>
</Form>
</Formik>
);
};
ReactDOM.render(<SignupForm />, document.querySelector("#root"));
Run Code Online (Sandbox Code Playgroud)
小智 7
Formik将初始值与name字段的 匹配,而不是id.
尝试使用它作为您的Field:
<Field name="email" id="email" type="email" />
import React from "react";
import ReactDOM from "react-dom";
import { Formik, Field, Form, ErrorMessage } from "formik";
import * as Yup from "yup";
import "./styles.css";
const SignupForm = () => {
return (
<Formik
initialValues={{ email: "" }}
validationSchema={Yup.object({
email: Yup.string()
.email("Mauvais e-mail")
.required("Champ requis")
})}
onSubmit={values => {
alert(JSON.stringify(values, null, 2));
}}
>
{props => (
<Form>
<label htmlFor="email">Email</label>
<ErrorMessage name="email" />
<br />
<Field
name="email"
id="email"
type="email"
onChange={e => {
props.setTouched({ email: true });
props.handleChange(e);
}}
/>
<button type="submit">OK</button>
</Form>
)}
</Formik>
);
};
ReactDOM.render(<SignupForm />, document.querySelector("#root"));
Run Code Online (Sandbox Code Playgroud)
您还可以查看文档以获取更多信息Field