Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

82
Vistas
Problem with setting state.index on click

I'm creating a webpage slide show. I created a file called SlideshowData.js where I exported an array of objects with all image links and ids. So using react I mapped through the photos and added dots to them. I created the dots and each time the photos switch the dot changes color from gray to black to indicate that the current photo is active.

The problem occurs now that I keep trying to figure out how to make them clickable. So for example you would click on the first dot it would bring you to the first slide. I tried to do it with a "set.state" function and set the initial index to the "event.target.key" which I assigned slide.id but it doesn't work.

Thank you for your time. I attached the code below.

class Slideshow extends React.Component{
constructor(props){
    super(props);
    this.state = {
        index: 0,
        delay: 5000,
        length: SlideshowData.length,
    }
    this.clickedDot = this.clickedDot.bind(this)
}


componentDidMount(){
    
    
    document.addEventListener("onClick", this.clickedDot)
    
    if(this.state.index === this.state.length -1){
        setTimeout(()=> this.setState(()=>({
            index: 0
        })),this.state.delay)
    }else{
        setTimeout(()=> this.setState((state)=>({
            index: state.index + 1,
        })),this.state.delay)
    }
    
}
componentDidUpdate(){
    document.addEventListener("onClick", this.clickedDot)
    
    if(this.state.index === this.state.length -1){
        setTimeout(()=> this.setState(()=>({
            index: 0
        })),this.state.delay)
    }else{
        setTimeout(()=> this.setState((state)=>({
            index: state.index + 1,
        })),this.state.delay)
    }
    
}
clickedDot(){
    this.setState((slide) =>({
        index: this.state.length
    }))
    console.log(this.state.index)
}
render(){
    return(
        <div className="slidesContainer">
            <div className="SlideshowSlider" style={{ transform: `translate3d(${-this.state.index * 100}%, 0, 0)` }}>
                {SlideshowData.map((slide,index) => (
                <img className="slides" src={slide.image} key={index} alt="travel"/>
                ))}
            </div>
            <div className = "slideshowDots">
                {SlideshowData.map((slide,idx) => (
                <div onClick={this.clickedDot} key ={slide.id} className={`slideDot${slide.id === this.state.index ? " active" : ""}`} ></div>
                ))}
            </div>
        </div>
    )
  }
}
export default Slideshow
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can pass the index inside your onClick function on the dots such as this:

<div onClick={(idx) => this.clickedDot(idx)} key ={slide.id} className={`slideDot${slide.id === this.state.index ? " active" : ""}`} >

and then use the index to change the slide accordingly in your clickedDot function.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda