0

When I render this in ReactJS, I get the error: "Objects are not valid as a React child (found: object with keys {Source, Value}). If you meant to render a collection of children, use an array instead.". But in my initial state, I init the key "list_ratings" as an array.

// this is in my constructor method
this.state = {list_ratings: []}

// this is somewhere else in my code    
let movieRatings = [
                            {"Source": "IMDB", "Value": "8"},
                            {"Source": "GDN", "Value": "6"},
                            {"Source": "The times", "Value": "7"}
                        ];

                        this.setState({
                            list_ratings: movieRatings
                        });

How can I solve this?

I render the list of ratings as follows:

<p>{this.state.list_ratings}</p>
Johnnybossboy
  • 191
  • 3
  • 18

2 Answers2

0

You try to render plain Object which is not valid:

<div>
  <p>{this.state.list_ratings}</p>
</div>

Fix it by rendering something valid like key strings:

<p>{this.state.list_ratings[0].Source + " " + this.state.list_ratings[0].Value}</p>

or map the whole list:

<div>
  {
    this.state.list_ratings.map(lR => 
     <p key={lR.Value}>{lR.Source + " " + lR.Value}</p>
    )
  }
</div>
Tomasz Mularczyk
  • 34,501
  • 19
  • 112
  • 166
0

You need to use map on movieRatings, to go through each each object

 const movieRatingsList = movieRatings.map((movieRating, i) =>
    <li key={i}> {movieRating.Source} : {movieRating.Value}  </li>
  );


render(){
 return(
   {this.movieRatingsList()}
 )
}
Gautam Naik
  • 8,990
  • 3
  • 27
  • 42