One of my latest projects involves creating a dynamic react slide show component using the following approach:
- The component accepts an array of slides (react components) as a prop and displays each slide one at a time, starting from the first slide with index 0.
- The current slide being displayed is visible while the other slides are hidden.
- During navigation (triggering the slide animation), the destination slide is added to the wrapper. If the destination slide has a smaller index than the current slide (when clicking on next), it will animate in from the left with a position change from -100% to 0 (slide-in effect from left). On the other hand, if the destination slide comes after the current slide (a larger index in the slides array, when clicking on previous), it will have an animation from 100% to 0 (slide-in effect from right); meanwhile, the current slide will slide out either to the right or left based on its position.
- Once the animation completes, the current slide will be swapped with the slided-in slide (the destination slide), and the previous current slide will be removed from view.
While working on the Slides component, I encountered a glitch where placing the destination slide before the current slide in the wrapper component caused flickering issues. However, positioning it after the current slide did not result in any problems.
I am curious to understand why this flickering behavior occurs. Can you help shed some light on this issue?
In the scenario where there is no flickering (animation slide placed after the current slide), here is a demo: https://stackblitz.com/edit/vitejs-vite-wnedwd?file=src%2FSlides.jsx
flickering issue observed when the animation slide precedes the current slide): demo: https://stackblitz.com/edit/vitejs-vite-hy6rsp?file=src%2FSlides.jsx