Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling error in React with Formik: Objects are not valid as a React child

I have the following formik login form. The form works fine except when handling the error raised for failed login attempts, I receive the following application error:

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.

I use setStatus(error); to show the error. Do you see any problems with the code below?

 <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>
like image 615
renakre Avatar asked Sep 06 '25 10:09

renakre


2 Answers

The issue lies here: <div className={'alert alert-danger'}>{status}</div>

status is an object (console.log it to check), not a string.

like image 167
Pierre V. Avatar answered Sep 09 '25 01:09

Pierre V.


The error is because status is an Object value.

React will not render this raw Object in virtual DOM. You need to convert or render only useful property.

<div className={'alert alert-danger'}>{JSON.stringify(status)}</div>
like image 24
Diamond Avatar answered Sep 09 '25 02:09

Diamond