0

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

Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
albert_anthony6
  • 594
  • 2
  • 9
  • 30
  • "I've tried brackets" - can you show this attempt? – Nick Parsons Feb 25 '20 at 05:18
  • 1
    Sure, one moment while I edit how I tried it – albert_anthony6 Feb 25 '20 at 05:19
  • It's at the bottom. I tried writing that way where it says 'popular' – albert_anthony6 Feb 25 '20 at 05:21
  • 3
    use `[category]` (computed property names) instead – Nick Parsons Feb 25 '20 at 05:21
  • 1
    So far so good. No red underlying syntax errors show. It shows that the parameter is being used now. I think that did it but won't know for sure until I test it. – albert_anthony6 Feb 25 '20 at 05:24
  • I have the issue of passing popular as plain text as the argument when calling the method. It cannot be a string because the name of the object (popular) shouldn't be a string, I get the error that popular is not defined when setting it as the category argument. – albert_anthony6 Feb 25 '20 at 05:59
  • the keys/properties inside objects can only be strings, or Symbols (which you're not using here). When you do {movies: ..., heroImage: ...} etc..., movies, heroImage are all treated as strings when the object literal is constructed. So, you'll have to pass `popular` as a string, which shouldn't cause an issue I believe – Nick Parsons Feb 25 '20 at 06:04
  • I get no error but it's also not being recognized – albert_anthony6 Feb 25 '20 at 06:07
  • what do you mean by it is not being recognized? – Nick Parsons Feb 25 '20 at 06:14
  • When I write popular within the setState as plain text, it works fine. But when I try using [category] to replace the 'popular' within the setState, I get an error that states that the argument 'popular' is not defined because i simply tried to invoke the method with popular being plain text which it won't allow. So I converted the argument to a string 'popular' and nothing is outputed as if it never reaches the object itself because the name isn't right or something. – albert_anthony6 Feb 25 '20 at 06:16
  • its a little difficult to tell what the issue could be from the code you've given alone. I guess you can try and debug using `console.log` to see what parts of the code are being reached (I believe the code should set the state fine) and what particular values are. – Nick Parsons Feb 25 '20 at 06:29
  • I've edited the code so you can see my initial state value and how I'm trying to invoke the method using two arguments. The method is being invoked within the searchMovies method. – albert_anthony6 Feb 25 '20 at 06:35
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/208467/discussion-between-nick-parsons-and-albert-anthony6). – Nick Parsons Feb 25 '20 at 06:40

0 Answers0