0

I have a little problem with my carousel, I just try to remove the right and left arrow when there is only one image but when I try to add this code, I have an error that tells me that "style" is undefined or null. I looked for hours but impossible to find the solution. Do you know where it can come from? Thanks.

The carousel :

https://github.com/AxSF3/Projet-11/blob/main/src/components/Carrousel/Carrousel.js

What I tried to add line 28 to the carousel :

const hideArrows = () =>  { 
   if (nombreImg === 1) {
     document.getElementsByClassName('fleche')[0].style.visibility = 'hidden';
   } else {
     document.getElementsByClassName('fleche')[0].style.visibility = 'visible';
   }

        
 }
 hideArrows()

I try to add this code :

const hideArrows = () =>  { 
   if (nombreImg === 1) {
     document.getElementsByClassName('fleche')[0].style.visibility = 'hidden';
   } else {
     document.getElementsByClassName('fleche')[0].style.visibility = 'visible';
   }

        
 }
 hideArrows()
AxCode
  • 1
  • 1
    Are you sure it says `style` is undefined or null or does it say that you are trying to read `style` *from* something that is undefined or null? Please **quote full error messages**; don't paraphrase them! – Quentin Mar 07 '23 at 15:54
  • `document.getElementsByClassName()` can return a list with no elements in it, which is very probably what's happening in your code. – Pointy Mar 07 '23 at 15:55
  • Please read [Something on my web site doesn't work. Can I just paste a link to it?](http://meta.stackoverflow.com/questions/125997/something-on-my-web-site-doesnt-work-can-i-just-paste-a-link-to-it). – Quentin Mar 07 '23 at 15:55
  • Don't do direct DOM manipulation in React. Instead change the state and generate the new DOM in the returned JSX. i.e. follow the examples of `imgPrecedente` and `imgSuivante` – Quentin Mar 07 '23 at 15:56

0 Answers0