1

Minimum Reproducible Example on Github (The images don't show in this MRE, but that's ok and doesn't matter because the problem I'm wondering about has nothing to do with that)


Problem: Every time the slide index changes, the screen scrolls to the top of the page. It happens automatically.

This happens when

  • incrementing the slide number by 1
  • when decrementing the slide number from 3->1 or from 2->0

But does not happen when

  • incrementing the slide number from slide 2 to slide 3 (slides numbered 0-3).
  • decrementing the slide number by 1.

Note: When slide 3 is the active slide that means that slide 3 is the one on top, and slide 1 and 2 are the ones on the bottom.

You can see what I'm talking about in this gif below.

  • Note: (The scrolling down that you see in the gif is done manually.)

enter image description here


Extra (In case it's useful)

I think that this may be caused by my useEffect function, which loops through each slide and assigns them to be visible or invisible and when they lay in the grid layout. When slide 4 is active, that means that slide 1 and slide 2 would be assigned to be the bottom slides first, slide 3 would then be assigned to be invisible and slide 4 would then be assigned to be the top slide.

  useEffect(() => {

    const slides = Array.from(document.getElementsByClassName('slide'))
    const dots = Array.from(document.getElementsByClassName('dot'))

    slides.forEach((slide, slideIdx) => {
      if(slideIdx === index){
        slide.className = "slide slide-un fade-in"
        dots[slideIdx].className = "dot activeDot"
        return
      } else if (slideIdx === index + 1 || slideIdx === 0 && index === squares.length - 1){
        slide.className = triple ? "slide slide-du fade-in" : "slide invisible"
      }else if (
        slideIdx === index + 2 || 
        (slideIdx === 0 && index === squares.length - 2) || 
        (slideIdx === 1 && index === squares.length - 1)
      ){
        slide.className = triple ? "slide slide-twa fade-in" : "slide invisible"
      }else{
        slide.className = "slide invisible"
      }
      dots[slideIdx].className = "dot"
    })

  }, [index])
Sam
  • 1,765
  • 11
  • 82
  • 176
  • https://stackoverflow.com/questions/63732257/carousel-causes-screen-to-move-to-top-of-page-on-slide-change – coreyward Sep 04 '20 at 19:26
  • @coreyward How does this contain multiple questions? I'm asking why does the screen move when the slide index increments – Sam Sep 04 '20 at 19:45
  • Again, you are not following even the most basic of React examples, and so your bugs are stemming from your refusal to do so. Debugging complex browser issues as a result of your refusal to use a framework the way it was intended is wholly out of scope. To quote the React docs I linked you to the last time you posted this question verbatim: “In our experience, thinking about how the UI should look at any given moment, rather than how to change it over time, eliminates a whole class of bugs.” – coreyward Sep 04 '20 at 21:13
  • 1
    @coreyward I'm not refusing to do anything. The thing you're telling me to do is unclear to me. Am I not supposed to update the css of a React element to change to `display: none`? That's the only thing I've been able to take out of that link that you shared. I'm sorry I'm not trying to be aggressive, I genuinely don't understand what advice you are trying to give me – Sam Sep 05 '20 at 03:55
  • 3
    You are manipulating the DOM directly instead of using React to render the changes. If you follow the React guides and tutorials you'll learn about component lifecycles and how to handle state. – coreyward Sep 05 '20 at 16:04
  • @coreyward Thanks I figured it out eventually. Btw I knew about state and I know about how react re-renders components when their state updates. I wasn't aware that manipulating the DOM is strictly off limits though, and also didn't really understand what you were talking about at first. There's another place in one of my websites where I do something similar, but it's so that I can insert ads in between `

    ` tags that are generated by markdown. I can't think of another way that this would be possible, but is this still a bad idea?

    – Sam Sep 08 '20 at 21:23
  • [This is a relevant video](https://www.youtube.com/watch?v=BYbgopx44vo) that talks about the topic coreyward was talking about for anyone else who encounters this question – Sam Sep 10 '20 at 02:07

1 Answers1

1

What may have cause the problem was that I was manipulating the DOM directly by using document.getElementsByClassName within my useEffect function, and then was setting each slide to be at a certain place in the grid, or making the slide invisible. I changed my code so that an array of slides was recorded in state

const [slides, setSlides] = triple ? useState(squares.slice(0,3)) : useState([squares[0]])

I then updated the state of slides within the useEffect function, instead of manipulating their className

  useEffect(() => {
    if (triple){
      if(index === squares.length - 1){
        setSlides([squares[index]].concat(squares.slice(0,2)))
      }else if(index === squares.length - 2){
        setSlides(squares.slice(index, index+2).concat([squares[0]]))
      }else{
        setSlides(squares.slice(index, index+3))
      }
    }else{
      setSlides([squares[index]])
    }
  }, [index])
Sam
  • 1,765
  • 11
  • 82
  • 176