question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The first time next arrow is clicked, it will behave as if I clicked previous and inserts an empty item into the carousel

See original GitHub issue

Hey there, I have an instance of react-responsive-carousel that is paging incorrectly on the first click of pagination when either at the beginning or the end of the items.

I have captured the behavior in a gif. Basically, the first time I click the next arrow, it will behave as if I clicked previous and act as if it’s inserting an empty item into the carousel and page in the wrong direction. Then when I reach the end of the items, it does it again in the opposite direction.

home-carousel

Here’s my code for context.

This first file is the general carousel component that will spit out the children we give it:

import "./carousel.scss";
import { bool, node, number, string } from "prop-types";
import { Carousel } from "react-responsive-carousel";
import React from "react";
import { homeCarouselPaged } from "../../../../../util/analytics/tracking";

export default class HomeCarousel extends React.Component {
  static propTypes = {
    children: node,
    centerMode: bool,
    centerSlidePercentage: number,
    className: string,
    showArrows: bool,
    showIndicators: bool,
    showStatus: bool,
    showThumbs: bool,
    trackingContext: string.isRequired
  };

  static defaultProps = {
    centerMode: false,
    centerSlidePercentage: 80,
    className: "",
    showArrows: false,
    showIndicators: true,
    showStatus: false,
    showThumbs: false
  };

  handleChange = () => {
    homeCarouselPaged(this.props.trackingContext);
  };

  render() {
    const {
      centerMode,
      centerSlidePercentage,
      className,
      showArrows,
      showIndicators,
      showStatus
    } = this.props;

    return (
      <Carousel
        centerMode={centerMode}
        centerSlidePercentage={centerSlidePercentage}
        className={className}
        dynamicHeight={false}
        // NOTE: Uncomment /emulateTouch/ for testing on PC
        emulateTouch
        onChange={this.handleChange}
        showArrows={showArrows}
        showIndicators={showIndicators}
        showStatus={showStatus}
        showThumbs={false}
      >
        {this.props.children}
      </Carousel>
    );
  }
}

The previous component is getting it’s children from here. I am mapping over a JSON object that gives me the 10 children that the Carousel uses:

import Carousel from "../carousel";
import { Link } from "react-router-dom";
import React from "react";
import { bool } from "prop-types";
import phrases from "./locales/en.yml";
import styles from "./styles.scss";
import { withSizes } from "../../../../../components/responsive/responsive";

@withSizes
export default class BrowsePhotos extends React.Component {
  static propTypes = {
    isSmScreen: bool,
    isMdScreen: bool,
    isLgScreen: bool
  };

  renderCards() {
    return phrases.text.categories.map(category => (
      <Link key={category.title} to={category.href}>
        <div className={styles.card}>
          <div className={styles.cardCover}>
            <img src={category.cover} />
          </div>
          <div className={styles.cardTitle}>{category.title}</div>
        </div>
      </Link>
    ));
  }

  render() {
    const { isSmScreen } = this.props;
    const slidePercentage = isSmScreen ? 45 : 20;

    return (
      <div className={styles.container}>
        <Carousel
          centerMode
          centerSlidePercentage={slidePercentage}
          className={styles.carousel}
          showArrows={!isSmScreen}
          showIndicators={false}
          trackingContext="browse photos"
        >
          {this.renderCards()}
        </Carousel>
      </div>
    );
  }
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8

github_iconTop GitHub Comments

3reactions
robabbycommented, Aug 28, 2019

@huykon @Sri-Linga My solution was to use react-slick instead.

https://www.npmjs.com/package/react-slick

1reaction
ArnoCoorevitscommented, May 14, 2020

I am still seeing this issue, any updates ? 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bootstrap 4 Carousel doesn't advance after the first Next ...
Once it does and I click on one of the next / previous arrow a second time, the carousel won't advance. I do...
Read more >
When adding previous and next arrow next to carousel ...
Open JSFiddle linked above · Click the next arrow multiple times and see the indicator not being marked as active correctly every second...
Read more >
how to let the first slide appear after button click also in antd ...
I am using a Bootstrap Carousel in my website and want to achieve this: The carousel should reset and start from the first...
Read more >
Introduction to browser events - The Modern JavaScript Tutorial
An event is a signal that something has happened. All DOM nodes generate such signals (but events are not limited to DOM).
Read more >
How to Use the Image Carousel Widget in Elementor - YouTube
In this tutorial, we learn how to use and configure the Image Carousel Widget. This highly customizable widget which allows you to display...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found