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>
2
  • how do you render list_ratings? Commented May 10, 2018 at 16:39
  • @madox2 I updated my post Commented May 10, 2018 at 16:40

2 Answers 2

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>
Sign up to request clarification or add additional context in comments.

Comments

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()}
 )
}

1 Comment

You need to wrap the render function

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.