Objects are not valid as a React child. If you meant to render a collection of children, use an array instead

前端 未结 11 2546
悲&欢浪女
悲&欢浪女 2020-11-28 07:28

I am setting up a React app with a Rails backend. I am getting the error \"Objects are not valid as a React child (found: object with keys {id, name, info, created_at, updat

相关标签:
11条回答
  • 2020-11-28 07:49

    Just to add to the other options, I was trying to access a nested object within the main object through the dot method as in: this.state.arrayData.CompleteAdress.Location In this case Location is a nested object inside Complete address which is why i cant simply access it with the dot notation.

    • So if you're facing this same issue, try JSON.parse so that you access the nested object and then manipulate accordingly.
    0 讨论(0)
  • 2020-11-28 07:51

    I hope it will help someone else.

    This error seems to occur also when you UNintentionally send an object to React child components.

    Example of it is passing to child component new Date('....') as follows:

     const data = {name: 'ABC', startDate: new Date('2011-11-11')}
     ...
     <GenInfo params={data}/>
    

    If you send it as value of a child component parameter you would be sending a complex Object and you may get the same error as stated above.

    Check if you are passing something similar (that generates Object under the hood).

    0 讨论(0)
  • 2020-11-28 07:56

    In your state, home is initialized as an array homes: []

    In your return, there is an attempt to render home (which is an array). <p>Stuff: {homes}</p>

    Cannot be done this way --- If you want to render it, you need to render an array into each single item. For example: using map()

    Ex: {home.map(item=>item)}

    0 讨论(0)
  • 2020-11-28 08:00

    Your data homes is an array, so you would have to iterate over the array using Array.prototype.map() for it to work.

    return (
        <div className="col">
          <h1>Mi Casa</h1>
          <p>This is my house y&apos;all!</p>
          {homes.map(home => <div>{home.name}</div>)}
        </div>
      );
    
    0 讨论(0)
  • 2020-11-28 08:00

    Although not specific to the answer, this error mostly occurs when you mistakenly using a JavaScript expression inside a JavaScript context using {}

    For example

    let x=5;
    
    export default function App(){ return( {x} ); };
    

    Correct way to do this would be

    let x=5;
    export default function App(){ return( x ); };
    
    0 讨论(0)
提交回复
热议问题