使用 Formik 处理 React 中的错误:Objects are not valid as a React child
Handling error in React with Formik: Objects are not valid as a React child
我有以下 formik
登录表单。该表单工作正常,除了在处理因登录尝试失败而引发的错误时,我收到以下应用程序错误:
Objects are not valid as a React child (found: Error: Request failed
with status code 500). If you meant to render a collection of
children, use an array instead.
我用setStatus(error);
来显示错误。你看到下面的代码有什么问题吗?
<div>
<h4>Login</h4>
<Formik
initialValues={{
username: '',
password: ''
}}
validationSchema={Yup.object().shape({
username: Yup.string().required('Username is required'),
password: Yup.string().required('Password is required')
})}
onSubmit={({ username, password }, { setStatus, setSubmitting }) => {
setStatus();
//authenticationService.login(username, password)
axios({
method: 'post',
url: 'api/ApplicationUser/Authenticate',
data: {
Username: username,
Password: password
}
}).then(
user => {
localStorage.setItem('currentUser', JSON.stringify(user));
currentUserSubject.next(user);
if (user !== null) {
if (user.data['roles'].result.includes('Admin'))
history.push('/admin/courses');
history.push('/home');
}
},
error => {
setSubmitting(false);
setStatus(error);
}
);
}}
render={({ errors, status, touched, isSubmitting }) => (
<Form>
<div className="form-group">
<label htmlFor="username">Username</label>
<Field name="username" type="text" className={'form-control' + (errors.username && touched.username ? ' is-invalid' : '')} />
<ErrorMessage name="username" component="div" className="invalid-feedback" />
</div>
<div className="form-group">
<label htmlFor="password">Password</label>
<Field name="password" type="password" className={'form-control' + (errors.password && touched.password ? ' is-invalid' : '')} />
<ErrorMessage name="password" component="div" className="invalid-feedback" />
</div>
<div className="form-group">
<button type="submit" className="btn btn-primary" disabled={isSubmitting}>Login</button>
{isSubmitting && <div>Loading...</div>}
</div>
{
status &&
<div className={'alert alert-danger'}>{status}</div>
}
</Form>
)}
/>
</div>
问题出在这里:<div className={'alert alert-danger'}>{status}</div>
status
是一个对象(console.log
它要检查),而不是字符串。
错误是因为 status
是一个对象值。
React
将不会在虚拟 DOM 中呈现此原始对象。您只需要转换或渲染有用的 属性.
<div className={'alert alert-danger'}>{JSON.stringify(status)}</div>
我有以下 formik
登录表单。该表单工作正常,除了在处理因登录尝试失败而引发的错误时,我收到以下应用程序错误:
Objects are not valid as a React child (found: Error: Request failed with status code 500). If you meant to render a collection of children, use an array instead.
我用setStatus(error);
来显示错误。你看到下面的代码有什么问题吗?
<div>
<h4>Login</h4>
<Formik
initialValues={{
username: '',
password: ''
}}
validationSchema={Yup.object().shape({
username: Yup.string().required('Username is required'),
password: Yup.string().required('Password is required')
})}
onSubmit={({ username, password }, { setStatus, setSubmitting }) => {
setStatus();
//authenticationService.login(username, password)
axios({
method: 'post',
url: 'api/ApplicationUser/Authenticate',
data: {
Username: username,
Password: password
}
}).then(
user => {
localStorage.setItem('currentUser', JSON.stringify(user));
currentUserSubject.next(user);
if (user !== null) {
if (user.data['roles'].result.includes('Admin'))
history.push('/admin/courses');
history.push('/home');
}
},
error => {
setSubmitting(false);
setStatus(error);
}
);
}}
render={({ errors, status, touched, isSubmitting }) => (
<Form>
<div className="form-group">
<label htmlFor="username">Username</label>
<Field name="username" type="text" className={'form-control' + (errors.username && touched.username ? ' is-invalid' : '')} />
<ErrorMessage name="username" component="div" className="invalid-feedback" />
</div>
<div className="form-group">
<label htmlFor="password">Password</label>
<Field name="password" type="password" className={'form-control' + (errors.password && touched.password ? ' is-invalid' : '')} />
<ErrorMessage name="password" component="div" className="invalid-feedback" />
</div>
<div className="form-group">
<button type="submit" className="btn btn-primary" disabled={isSubmitting}>Login</button>
{isSubmitting && <div>Loading...</div>}
</div>
{
status &&
<div className={'alert alert-danger'}>{status}</div>
}
</Form>
)}
/>
</div>
问题出在这里:<div className={'alert alert-danger'}>{status}</div>
status
是一个对象(console.log
它要检查),而不是字符串。
错误是因为 status
是一个对象值。
React
将不会在虚拟 DOM 中呈现此原始对象。您只需要转换或渲染有用的 属性.
<div className={'alert alert-danger'}>{JSON.stringify(status)}</div>