Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

241
Views
Error: Objects are not valid as a React child (found: object with keys {inputList}). If you meant to render a collection of children, use an array

I was trying object to create input fields using key and value pair. I thought it would be a breeze but I am getting this error. I find no easy way of doing this thing but this.

import {Form, Input, Button } from 'semantic-ui-react'

const input = [{
    name: 'Full Name', 
    dob: 'Date of Birth', 
    parentName: "Father's/Mother's Name", 
    email: "Email",
    mobile: "Mobile Number",
    password: "Password",
    rePassword: "Re-enter Password",
    aadhar: "Aadhar Number"
}]

// const inputList = ["Full Name", "Date of Birth", "Father's/Mother's Name", "Email", "Mobile Number","Password", "Re-enter Password", "Aadhar"]

let inputList = [];

for(let key in input) {
    inputList.push(<Form.Field name={key} key={key} control={Input} label={input[key]} />)
}

const RegistrationForm = () => {
    return(
        <Form>
            <Form.Field name="name" control={Input} placeholder='Full Name' label='Full Name:'/>
            {
                {inputList}
                // inputList.map((input,index) => {
                //     return(<Form.Field key={index} control={Input} label={input}/>)
                // })
            }
        </Form>
    )
}

export default RegistrationForm

So what I am trying to do here is, create an object where my key would be the name of the form field and the value would be the label. To use that I was using for loop but I am getting the above mentioned error. How to resolve this ? If not, is there any other way to get this done.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The problem with your code is that when rendering to the DOM, React will convert JSX to HTML DOM element (you can read more here). So anything inside the curly bracket, React will assume that it is embedded JS code that is expecting to return JSX, an Array of JSX or a value. By doing {{inputList}} what you are trying to do is telling React to render a JS Object with key, ES6 syntax which you can read more here, inputList into the DOM which fundamentally illegal to do so in React. So since you're already do the creation of JSX for each element in inputList in your for loop, you can just do {inputList} instead like this:

const RegistrationForm = () => {
    return(
        <Form>
            <Form.Field name="name" control={Input} placeholder='Full Name' label='Full Name:'/>
            {inputList}
        </Form>
    )
}

On another note, if you don't want to use for loop, what you can do instead is this:

let inputList = Object.entries(input).map(([key, value]) => (<Form.Field name={key} key={key} control={Input} label={value} />));

You can read more on Object.entries here

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!