disclaimer: I have checked other threads such as this one and can see that my issue is not technically the same:Promise Error: Objects are not valid as a React child
I have sent my api request:
async getGenreList(){
const genresResults = await getGenres()
return genresResults
}
componentDidMount() {
this.getGenreList().then((response) => {
this.setState({ genreOptions: response})
});
}
here is the response: each object includes an id and name key
(19) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
in the state genreOptions is expecting an array
this.state = {
totalCount: 0,
genreOptions: [],
};
I pass genreOptions
to the MovieList
component where I then map over the genreOptions
<MovieList movies={results || []} genres={genreOptions || []} />
inside MovieList component
{genres.map((genre)=>{
return <div>{genre.id} {genre.name}</div>
})}
I am mapping over the array of objects that I got back from my Promise, so I am not sure why I am getting the error. Error: Objects are not valid as a React child (found: object with keys {id, name}). If you meant to render a collection of children, use an array instead.
Please let me know if you spot it.
Thanks in advance.
UPDATE TO SHOW WHAT ONE OF OBJECTS LOOKS LIKE
0:
id: 28
name: "Action"
__proto__: Object
UPDATE TO SHOW FULL MOVIELIST COMPONENET
export default class MovieList extends React.Component {
render() {
const { movies, genres } = this.props;
return (
<MoviesWrapper>
{movies.map((movie) => {
return <div>{movie.title}</div>;
})}
{genres.map((genre)=>{
return <div>{genre.id} {genre.name}</div>
})}
</MoviesWrapper>
);
}
}
*** a solution i came up with but not sure that i should use .map when updating the state ***
this.getGenreList().then((response) => {
this.setState({ genreOptions: response.map((genre) => genre.name)})
});
This returns an array of names