elvezet13
elvezet13

Reputation: 27

Objects are not valid as a React child?

The following function sets the state of my React app, personData being an object:

request('https://swapi.co/api/people/?search='+event.target.value, (error,response,body) => {
  this.setState({personData: JSON.parse(body)});
  console.log(this.state)
})

Then in the render function:

     <td>{this.state.personData}</td>

I get the error 'Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead.'. How can I fix this?

Upvotes: 0

Views: 809

Answers (1)

Morleee
Morleee

Reputation: 347

If you are wanting to render the JSON response, you will have to stringify the response

<td>JSON.stringify(this.state.personData, null, 2)</td>

Upvotes: 1

Related Questions