A review of sliders with
-
Click the right/left button to show the next/previous slider.
-
Use
setInterval()
to make slider automatically changes. -
If clicking the right/left buttons, clean previous
setInterval()
.
.
├── README.md
└── src
├── App.js
├── components
│ ├── Article.js
│ ├── ButtonLeft.js
│ ├── ButtonRight.js
│ └── Title.js
├── data.js
├── index.css
└── index.js
Display all sliders with flex to make them in the same row and use CSS transform: translateX();
to adjust the position of every slider.
Add class with 'lastSlide', 'activeSlide' and 'nextSlide', it represent from left, middle, and to the right.
Put all slides on the right-hand side first, when the person index matches the index state, change the class of the person index from 'nextSlide' into 'activeSlide'
Use setIndex(index - 1)
and setIndex(index + 1)
to switch the slides, and useEffect()
to control the edge case:
useEffect(() => {
const lastIndex = people.length - 1;
if (index < 0) {
setIndex(lastIndex);
}
if (index > lastIndex) {
setIndex(0);
}
}, [index, people]);
setInterval()
with index + 1 to make the slides move automatically
useEffect(() => {
setInterval(() => {
setIndex(index + 1);
}, 3000);
}, [index]);
Get Return value from setInterval()
and when clicking the next or previous button, it cleared the previous setInterval(
) by clearInterval()
.
In the project directory, you can run:
Runs the app in the development mode.
Open http://localhost:3000 to view it in your browser.