Giter Site home page Giter Site logo

Comments (2)

DZuz14 avatar DZuz14 commented on July 30, 2024

Yeah, you are totally right. I never thought of that. This is actually an interesting problem to solve, as the code definitely needs a refactoring to support this. Any ideas?

from react-hooks-image-slider.

NicolaG1993 avatar NicolaG1993 commented on July 30, 2024

This is how I've solved this issue in my App:

(Slider.js)

  • delete play, interval, the if statement that activate setInterval, and the clearInterval as well
  • write them inside a new useEffect
  • inside the array pass activeSlide

All of this code now gets activated only when a new slide renders on screen, and this will also clear the interval when the component unmount

I haven't tested it so much yet and i'm new to React too, so im not 100% sure this is the best solution, but it seems to work!

Btw thanks a lot @DZuz14! I've founded this slider very usefull for my project, so I really hope i can give something back with this solution

here is the code:
useEffect(() => { const play = () => { autoPlayRef.current(); }; let interval = null; if (props.autoPlay) { interval = setInterval(play, props.autoPlay * 1000); } return () => { if (props.autoPlay) { clearInterval(interval); } }; }, [activeSlide]);

from react-hooks-image-slider.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.