I have a problem with the automatic content change. After 7 seconds it should change automatically by 1 (and so it works). The problem is that when the user clicks on the first card, after the carousel automatically goes to the second card, the interval, which is already running, completes its cycle and increases the index, causing the carousel to jump to the third card, not the the second etc.
useEffect(() => {
intervalRef.current = setInterval(() => {
const nextIndex = (activeIndex + 1) % expertise.length;
setActiveIndex(nextIndex);
;
}, 7000);
return () => clearInterval(intervalRef.current);
}, [activeIndex]);
const handleCardClick = (index) => {
setActiveIndex(index);
clearInterval(intervalRef.current);
};
I would like to see it work this way. If the content automatically changes to, for example, the fourth card, and the user then changes to the first, then after 7 seconds it should change to the second, and so on.
3
Answers
Whenever a user clicks a card, you can clear the interval and start it again. That way it will always be a full 7 seconds between every card change. I think that was your problem?
The other interpretation i can see of your question is that you are having trouble synchronizing the active index variable. I don’t think that you should have an issue with the code above, I think it should stay synced, but if you are having trouble you could start using a ref to track the current index:
If you do this, the interval handler will always know what the active index is.
You need to reset the interval whenever the user manually changes the card. This can be done by clearing the existing interval and setting a new one each time the active index changes, whether automatically or manually
Simply use the functional form of
setState
so your capturedactiveIndex
reference isn’t stale.