I'm trying to alter selected categorized objects within my state object using a parameter whenever I call my fetchMovies method. The goal is to focus on the 'popular' object or the 'new-releases' object (depending on what was passed as the category when the method gets invoked). Here is my code.
fetchMovies = async(endpoint, category) => {
this.setState({
error: false,
loading: true
});
const isLoadMore = endpoint.search('page');
try {
const result = await (await fetch(endpoint)).json();
this.setState(prev => ({
...prev,
data: {
movies: isLoadMore !== 1 ? {
...prev.data.movies,
popular: [...prev.data.movies.popular, ...result.results]
} : {
...prev.data.movies,
popular: [...result.results]
},
heroImage: prev.data.heroImage || result.results[0],
currentPage: result.page,
totalPages: result.total_pages
}
}));
} catch (error) {
I have tried the following but these don't seem to work:
{category} (Didn't work)
`${category}` (Didn't work)
What I need help figuring out is within the movies in the setState. Where it says "popular", I'm looking to replace 'popular' with the category parameter. I've tried brackets and template literals in attempts to work this out but nothing has worked so far. Any help figuring this out would be greatly appreciated. Thanks