遵循本教程https://www.youtube.com/watch?v=yNiJkjEwmpw&t=1578s关于 formik 表单验证并在实施验证时遇到问题
validationSchema: Yup.object().shape({
email: Yup.string().email().required(),
password: Yup.string().min(6).required()
}),
我在本地环境中没有收到任何错误消息,但在我的代码框上我收到以下错误TypeError
Cannot read property 'object' of undefined
这很简单,但我的知识不足以调试(对 react 和 formik 来说仍然是新手)感谢所有帮助,下面是完整的源代码&这里是代码和框的链接
https://codesandbox.io/s/kw4x2k62rv
import React from 'react';
//import logo from './logo.svg';
import './App.css';
import { Button, Form, FormGroup, Label, Input, FormFeedback } from 'reactstrap';
import { withFormik, Field } from 'formik';
import Yup from 'yup';
const App = ({
values,
handleChange,
handleSubmit,
errors,
touched
}) =>
(
<Form onSubmit={handleSubmit}>
<FormGroup>
<Label for="fname">First Name</Label>
<Input
value={values.fname}
onChange={handleChange}
type="text"
name="fname"
id="fname"
placeholder="first name"
autoComplete="first name"
/>
</FormGroup>
<FormGroup>
<Label for="lname">Last Name</Label>
<Input
onChange={handleChange}
value={values.lname}
type="text"
name="lname"
id="lname"
placeholder="last name"
autoComplete="last name"
/>
</FormGroup>
<FormGroup>
<Label for="email">Email</Label>
<Input
onChange={handleChange}
value={values.email}
type="email"
name="email"
id="email"
placeholder="email"
autoComplete="email"
/>
{ touched.email && errors.email && <FormFeedback>Oh noes! that name is already taken</FormFeedback> }
</FormGroup>
<FormGroup>
<Label for="password">Password</Label>
<Input
onChange={handleChange}
type="password"
value={values.password}
name="password"
id="password"
placeholder="password"
autoComplete="password"
/>
</FormGroup>
<FormGroup>
<Label for="confirmPassword">Confirm Password</Label>
<Input
onChange={handleChange}
value={values.confirmPassword}
type="password"
name="confirmPassword"
id="confirmPassword"
placeholder="confirm password"
autoComplete="confirm password"
/>
</FormGroup>
<FormGroup>
<Label>
<Field type="checkbox" name="tos" checked={values.tos} />
Check me out
</Label>
</FormGroup>
<Button> Submit </Button>
</Form>
)
const FormikApp = withFormik({
mapPropsToValues({ email, password, fname, lname, confirmPassword, tos }) {
return {
email: email || '',
password: password || '',
confirmPassword: confirmPassword || '',
fname: fname || '',
lname: lname || '',
tos: tos || false
}
},
validationSchema: Yup.object().shape({
email: Yup.string().email().required(),
password: Yup.string().min(6).required()
}),
handleSubmit(values) {
console.log(values);
}
})(App)
export default FormikApp;