I'm attempting to push JSON objects into an array every time a button is pressed and displayed the results

So i'm attempting to generate a random selection from an array which a user can add to by pressing a button and i'm a bit confused on how to do it.The app is made in react and it's used movieDB's api to make a film search engine. This is what I attempted so far:

import React from 'react';
import App from './App';
import{ Button } from 'reactstrap'
import { AddToSelector } from './Components/AddToSelector.Component'
class MovieRow extends React.Component{

  addToFavourites = () =>
    {
      var arr = [];
      const movieRow = <AddToSelector key={this.props.movie.id} 
      movie={this.props.movie}/>
      console.log(this.props.movie.title);
      arr = arr.push(this.props.movie.id);

    } 

    pickRandom = (arr) =>
    {
       console.log(arr);  
    }

  render() {
        return (
        <div>

        <table key = {this.props.movie.id}>
      <tbody>
        <tr>
          <td>
            <img alt="poster" src={this.props.movie.poster_src}/> 
          </td>
            <td>
            <p>{this.props.movie.title}</p>
            <p>{this.props.movie.overview}</p>
            <Button color="primary" onClick={this.addToFavourites}>Add</Button> 
            <Button color="primary" onClick={this.pickRandom}>Random</Button> 
          </td>
        </tr>
      </tbody>

    </table>



        </div>
        );
    }
}

export default MovieRow;

You're getting there. You've got a few issues:

  1. You're defining arr in addToFavourites, so every time it is called a new arr is created that's only available within that function. Essentially, you're creating the array, adding the item, and then it's tossed in the garbage.

    Since your MovieRow represents one movie, you're gonna want to have a wrapper component that maintains arr as part of its state. You'll also define addToFavourites there and then pass that down to each MovieRow component, making sure to pass at least the movie id so you know which movie to add to the favorites

  2. I'm not sure what movieRow in addToFavorites does because you assign it and then never use it. Essentially here you're defining movieRow on the global window object the first time around and then reassigning that each time you click. This seems unnecessary.

  3. You'll also want to move pickRandom to the parent component that maintains your favorite list.

Hopefully this gets you going in the right direction. Let me know if you have questions

Lauv - I Like Me Better [Official Video], Lauv's debut album, ~how i'm feeling~ is available now: http://lauv.lnk.to/ howimfeelingYD I Duration: 3:26 Posted: Aug 10, 2017 Í, í (i - acute) is a letter in the Faroese, Hungarian, Icelandic, Czech, Slovak, and Tatar languages, where it often indicates a long /i/ vowel (ee in English word feel). This form also appears in Catalan, Irish, Italian, Occitan, Portuguese, Spanish, Galician, Leonese, Navajo, and Vietnamese language as a variant of the letter “i”.

I think state is what you're probably looking for. You need to set initial array state in constructor, then update it in addToFavourites (using this.setState) and use updated state in pickRandom. Please consult the docs.

I (Kendrick Lamar song), I definition, the ninth letter of the English alphabet, a vowel. See more. Definition of -i- (Entry 8 of 8) —used as a connective vowel to join word elements especially of Latin origin matrilineal raticide me or I ?: Usage Guide Example Sentences Learn More about i

You should store the list of movies in you component state. Read more about the app state and components lifecycle in the React documentation

class MovieRow extends React.Component {
     constructor(props) {
        this.state = {
            movies: []
        }
     }
}

Then in the method that is handling the click event in your Add button, you can use setState() to update the state and then changes will be reflected in the template.

addToFavourites() {
  const newMovie = getMovie(); // I don't know where are you getting the movie, but you get the idea.
  this.setState((state) => {
    return { 
      movies: [...state.movies, newMovie] 
    }
  });
} 

Then considering you have stored in the state an array of objects representing your movies. You should map that array to a list of components in the render function.

Example:

render() {
  return (
    this.state.movies.map((movie) => <MovieComponent key={movie.id} title={movie.title} />)
  )
}

I, Windows is either a registered trademark or a trademark of Microsoft Corporation in the United States and/or other countries. Mac is a trademark of Apple Inc. In some sans serif typefaces, the uppercase letter I, 'I' may be difficult to distinguish from the lowercase letter L, 'l', the vertical bar character '|', or the digit one '1'. In serifed typefaces, the capital form of the letter has both a baseline and a cap-height serif, while the lowercase L generally has a hooked ascender and a baseline serif.

[PDF] USCIS Form I-9, Create an account or log in to Instagram - A simple, fun & creative way to capture, edit & share photos, videos & messages with friends & family. Ï, lowercase ï, is a symbol used in various languages written with the Latin alphabet; it can be read as the letter I with diaeresis or I-umlaut.. In Afrikaans, Catalan, Dutch, French, Galician, Welsh, Southern Sami, and occasionally English, ï is used when i follows another vowel and indicates hiatus (diaeresis) in the pronunciation of such a word.

Instagram, I definition is - the 9th letter of the English alphabet. How to use i in a sentence. me or I? Log in to i-Ready®, online assessment and instruction that helps teachers provide all students a path to proficiency and growth in reading and mathematics.

I, The ninth letter of the basic modern Latin alphabet. I (lower case ı). The letter i without a dot above, in both the upper case and the lower case versions. Bir hesap oluştur veya Instagram'a giriş yap - Fotoğraf ve videolar çekip düzenlemenin, bu fotoğrafları, videoları ve mesajları arkadaşlarınla ve akrabalarınla paylaşmanın basit, eğlenceli ve yaratıcı yolu.

Comments
  • var arr = []; only exists inside that function, and is deleted from memory when the function has finished. Move the declaration outside your class to keep the variable in memory. Also arr.push(...) is enough, arr = arr.push(...) will overwrite the array with the length of the new array. Next you'll want to remove the arr parameter from pickRandom since it otherwise shadows the outer arr. You don't need it there anyway, and you aren't passing anything into it to boot when you call the function.
  • Ok so i've done the first two steps but i'm a bit confused on the 3 piece of code, could you explain what you mean by mapping the array to a list of components? So far my array adds a movie to but it's still just one movie, which is the last one clicked so i'm a little stumped on how to approach it. Thanks a million for the answer!
  • If you are not familiar with array manipulation in Javascript, you should read more about it, there are plenty of tutorials, books, etc. The map() method creates a new array with the value returned in the function for every item in the array. In other words, we want to transform the raw data that you had from your .json into a list of React components to render in your template.
  • By the way, my answer is based on an array because I thought you wanted to display a list of movies but if you only want to show one movie, you could do it without an array.