1

How do I make my carousel to slide automatically without the use of left and right arrow. The left and right Method is working already.

This is the code I have so far:

function Slider() {
  let Arry = [
    <img src="\img\slide1.jpg" />,
    <img src="\img\slide2.jpg" />,
    <img src="\img\slide3.jpg" />
  ];
  const [x, setX] = useState(0);
  useEffect(() => {
    setX === 0 ? -100 * Arry.lenght-- : Arry.lenght++;
    setTimeout(x, 2000);
  });
  const goLeft = () => {
    x === 0 ? setX(-100 * (Arry.length - 1)) : setX(x + 100);
    console.log(x);
  };
  const goRight = () => {
    x === -100 * (Arry.length - 1) ? setX(0) : setX(x - 100);
    console.log(x);
  };
}
skyboyer
  • 22,209
  • 7
  • 57
  • 64
Ayomikun
  • 27
  • 4
  • Please show the code you have so far – Brandon Jan 17 '20 at 13:53
  • function Slider(){ let Arry =[ , , ]; const [x, setX] = useState(0); useEffect(()=>{ setX===0?-100*(Arry.lenght--):(Arry.lenght++) setTimeout(x,2000) }) const goLeft=()=>{ (x ===0)?setX(-100*(Arry.length-1)):setX(x+100) console.log(x) }; const goRight=()=>{ (x === - 100 *(Arry.length-1))? setX(0):setX(x-100) console.log(x) } – Ayomikun Jan 17 '20 at 13:55

1 Answers1

1

you can use setInterval in your on-mount effect and change the state in that function.