const [currentIndex, setCurrentIndex] = useState(0);
const [previousIndex, setPreviousIndex] = useState(2);
const [nextIndex, setNextIndex] = useState(1);
const previous = () = {
const newCurrentIndex = currentIndex === 0 ? slides.length - 1 : currentIndex - 1;
setCurrentIndex(newCurrentIndex);
const newPreviousIndex = previousIndex === 0 ? slides.length - 1 : previousIndex - 1;
setPreviousIndex(newPreviousIndex);
const newNextIndex = nextIndex === 0 ? slides.length - 1 : nextIndex - 1;
setNextIndex(newNextIndex);
}
const next = () = {
const newCurrentIndex = currentIndex === slides.length - 1 ? 0 : currentIndex + 1;
setCurrentIndex(newCurrentIndex);
const newPreviousIndex = previousIndex === slides.length - 1 ? 0 : previousIndex + 1;
setPreviousIndex(newPreviousIndex);
const newNextIndex = nextIndex === slides.length - 1 ? 0 : nextIndex + 1;
setNextIndex(newNextIndex);
}
It is used to render pictures depending on the index, the carousel displays 3 pictures at a time so I needed previous current and next index, but I don't know how to make write the logic so that I don't have to write the same thing 3 times but with different starting indexes.