-1

I have a React Carousel with 4 images for example.

import React from 'react'
import { FacebookShareButton } from "react-share";

import img1 from './images/kitten/200.jpg'
import img2 from './images/kitten/201.jpg'
import img3 from './images/kitten/202.jpg'
import img4 from './images/kitten/203.jpg'

const imageList = [img1, img2, img3, img4]

 class App extends Component {
   constructor(props) {
     super(props)
     this.selected = "";
 }

 render() {
   return (
     <div>
      <Carousel>
         {imageList.map((each) => (
         <img src{each} />
         ))}
      </Carousel>
     </div>
    )
  }
}
export default App;

I would like to pick one image from the slider

And push it in new variable defined above called this.selected

Thanks

Dev01
  • 149
  • 1
  • 9

1 Answers1

1

By your suggestions, is this what you want?

import React from 'react'
import { FacebookShareButton } from "react-share";

import img1 from './images/kitten/200.jpg'
import img2 from './images/kitten/201.jpg'
import img3 from './images/kitten/202.jpg'
import img4 from './images/kitten/203.jpg'

const imageList = [img1, img2, img3, img4]
class App extends Component {
 constructor(props) {
     super(props)
     this.state = {selected: ""};
 }
 pickFromCarousel(image) {
     this.setState({selected: image});
 }

 render() {
   return (
     <div>
      <Carousel>
         {imageList.map((each) => (
         <img onClick={() => this.pickFromCarousel(each)} src={each} />
         ))}
      </Carousel>
     </div>
    )
  }
}
export default App;

using state.

What I am doing is adding a selected to state in the constructor, and, of course, setting it to null first. We add a function called pickFromCarousel which says to set selected to the image requested. Then, in our render function, we use pickFromCarousel as our function when the image is clicked, by use of an arrow function. The rest of the code is practically the same as yours.

new Q Open Wid
  • 2,225
  • 2
  • 18
  • 34
  • https://codesandbox.io/s/agitated-sara-rln7f?file=/src/App.js it has an error please check attached, may i ask why the function name is addToCarousel where it should be pick from carousel because that is exactly what i want to achive – Dev01 Jan 02 '21 at 21:07
  • @Dev01 because your post is confusing. – new Q Open Wid Jan 02 '21 at 21:18
  • I have already stated that in the Title nevermind ? did u check the link ? – Dev01 Jan 02 '21 at 21:19
  • yes. of course I did. I am trying to solve the problem. – new Q Open Wid Jan 02 '21 at 21:21
  • Ok I appreciate it – Dev01 Jan 02 '21 at 21:23
  • https://codesandbox.io/s/agitated-sara-rln7f?file=/src/App.js this is working now. The problem was we were passing the function call to the image onClick, instead, it should be onClick={() => this.pickFromCarousel(each)} but there is still a problem the image carousel are rendered twice now ? i don`t know why – Dev01 Jan 02 '21 at 21:33
  • Can you show me a picture? Because I'm stuck at an error that says ``t is undefined``, just right now. – new Q Open Wid Jan 02 '21 at 21:36
  • https://codesandbox.io/s/agitated-sara-rln7f?file=/src/App.js open this link it is updated, I would like yours as well can u send me please thanks – Dev01 Jan 02 '21 at 21:40
  • Mine is this: https://codesandbox.io/s/cocky-sky-vewtq?file=/src/index.js – new Q Open Wid Jan 02 '21 at 21:41
  • Also, if you selected any image *other than the first one* there's only one carousel. – new Q Open Wid Jan 02 '21 at 21:45
  • Yes you are right I don`t know why i also faced the same problem when i used the ImagePicker it is better but unfortunately the error "Cannot read property map of undefined" prevent me from using it – Dev01 Jan 02 '21 at 21:51
  • (You can ask that as a separate question; for now please accept my answer)... (click the gray checkmark) – new Q Open Wid Jan 02 '21 at 21:55
  • As said, click the gray checkmark to accept @Dev01 – new Q Open Wid Jan 02 '21 at 22:01
  • I would rather wait for a few more days because that is the way it should be if there is no better answer I will accept yours. Thank you – Dev01 Jan 02 '21 at 22:12