0

i am getting below error when use style in my react code

 TypeError: Cannot read property 'style' of undefined 

i try to call the showSlides function . Error throws in style.

slides[this.slideIndex-1].style.display = "block"; 

code is :

showSlides(n) {
  var i;
  var slides = document.getElementsByClassName("mySlides");
  var dots = document.getElementsByClassName("dot");

  if (n > slides.length) {this.slideIndex = 1} 
  if (n < 1) {this.slideIndex = slides.length}
  for (i = 0; i < slides.length; i++) {
    slides[i].style.display = "none";  
  }
  this.slideIndex++;
  if (this.slideIndex > slides.length) {this.slideIndex = 1}    
  for (i = 0; i < dots.length; i++) {
    dots[i].className = dots[i].className.replace(" active", "");
  }
  slides[this.slideIndex-1].style.display = "block";  
  dots[this.slideIndex-1].className += " active";
  setTimeout(this.showSlides, 2000); // Change image every 2 seconds  
}

any syntax change in react js

Anthony Kong
  • 37,791
  • 46
  • 172
  • 304
John Ken
  • 898
  • 1
  • 13
  • 26

0 Answers0