mardi 19 mai 2020

modify an upcoming boolean inside an array in the if statement

The first item in the array of objects has it's state as isNext true. The rest are all false. I am using an if statement and want to turn the current boolean false and the next one true. I basically want to only call one image at a time once I hit the submit button. (haven't gotten to the button yet). I know the below function is a mess, but that is basically how I am conceiving of the issue. I am not sure how to use i to manipulate both the current and upcoming iteration of the array. Any help would be greatly appreciated!

generateImage() {
 return this.state.images.map((image, i) => {
   if(image[i].isNext === true) {
     image[i].isNext = false
     image[i + 1].isNext = true;
    return  <Image src={image.img} index={i} />
   }
 });
}

this.state

 images: [
          {
          img: 'https://uploads3.wikiart.org/images/wladyslaw-strzeminski/cover-for-a-book-by-julian-przybo-z-ponad-1930.jpg!Large.jpg',
          thought: '',
          isNext: true
        },
        {
          img: 'https://uploads6.wikiart.org/images/pablo-picasso/girl-on-the-ball-1905.jpg!Large.jpg',
          thought: '',
          isNext: false
        },
        {
          img: 'https://uploads8.wikiart.org/images/salvador-dali/et-post-buccellam-introivit-in-eum-satanas-psalms-40-10-1964.jpg',
          thought: '',
          isNext: false
        },
      ]

Aucun commentaire:

Enregistrer un commentaire