question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

react-form trouble with api validation | Objects are not valid as a React child

See original GitHub issue

Describe the bug So, I have an api which is consumed by the react-hook-form at the frontend. I contact the api for validation and in turn the api returns errors in the form. So What I want to do with those errors is set those errors to the respective form fields.

Codesandbox link (Required) Include a codesandbox will help us to investigate the issue quicker.

Expected behavior I expect the error given by the api to be added as a field error

Desktop (please complete the following information):

  • OS: Windows
  • Browser Chrome
  • Version 22

Additional context The api response format is like this:

error: {
field_name : error_obj
}

This is how I set the errors

export default function RegistrationForm () {
    const { register, handleSubmit, setError, errors } = useForm();
    const onSubmit = data => {
    axios.post('/api/register/', data)
        .then(response =>{
            const token = response.data.token;
            localStorage.setItem('token', token);
            //setJwt(token);
        })
        .catch(error =>{
            const api_errors = error.response.data.error
            for (let field_property in api_errors) {
                console.log(api_errors);
                console.log(api_errors[field_property]);
                console.log(api_errors[field_property][0]);
                setError(field_property, "manual", api_errors[field_property][0]);

            console.log(errors);
            console.log(errors.email);
            }
        });
    }

However, when I try to set the errors like that, I get this error:

Objects are not valid as a React child (found: object with keys {0, 1, 2, 3, 4, 5, ref}). If you meant to render a collection of children, use an array instead.

Why am I getting this error? This is the actual error object returned by the api:

{email: Array(1), username: Array(1), password: Array(1), last_name: Array(1), first_name: Array(1)}
email: ["This field is required."]
first_name: ["This field is required."]
last_name: ["This field is required."]
password: ["This field is required."]

Any suggestions? Can someone please help me fix this issue? thanks!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

8reactions
aryaniyapscommented, Nov 21, 2020

fixed!

this is what was the problem: there wasn’t any proper provision to display the errors

after adding this to each field, it was fixed:

<h3>Email</h3>{errors.email && <span className="errorMsg">{errors.email.message}</span>}
                <input name= "email" type="email" className={errors.email ? "errorInput" : "formInput"}
                ref={register()} />

thanks a lot @bluebill1049 and also I saw lots of unanswered questions on the internet regarding this could you mark this as the solution?

4reactions
bluebill1049commented, Nov 21, 2020

please read the doc and follow the API:

https://react-hook-form.com/api#setError

here is an example CSB: https://codesandbox.io/s/react-hook-form-v6-seterror-9cebt

Read more comments on GitHub >

github_iconTop Results From Across the Web

Resolving Error: "Objects are not valid as a React child"
As the error message states, React cannot render objects as children directly. If you are seeing this error, there is a good chance...
Read more >
Objects are not valid as a React child (found: object with keys ...
I am trying to validate my form field with Formik , but I am getting the error Objects are not valid as a...
Read more >
uncaught error: objects are not valid as a react child (found
It looks like your error rendering is fine (i.e., in your conditional, you either render a boolean or a string [message/toString()]). The problem...
Read more >
Objects are not valid as a React child : r/learnjavascript - Reddit
react-dom.development.js:14887 Uncaught Error : Objects are not valid as a React child (found: object with keys {name, id}).
Read more >
Test Renderer - React
This package provides a React renderer that can be used to render React components to pure JavaScript objects, without depending on the DOM...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found