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.

Reset slider position on every render of carousel

See original GitHub issue

#18 hey, I am facing an issue with react-responsive-carousel.

I have a parent component :

//Main

 return (
    <>
        <Wrapper>
          <UserDisplay
            user={currentUserDisplay}
            handleLike={handleLike}
            handleUnLike={handleUnLike}
          />
        </Wrapper>
    </>
  );

and a child component: //UserDisplay

const UserDisplay = ({ user, handleLike, handleUnLike }) => {

  const { firstName, images } = user;
      <div>
        <Carousel
          infiniteLoop={true}
          showStatus={false}
          showArrows={false}
          showThumbs={false}
          selectedItem={0}
        >
          {images.map(({ img }, i) => (
            <div className="item" key={i}>
              <ProfilePicture urlPath={img}>
                <Text>{`${firstName}`}</Text>
              </ProfilePicture>
            </div>
          ))}
        </Carousel>
      </div>
};

Whenever a user changes at parent component , different images will be displayed at the carousel, as expected. My problem is , the slider’s position from previous user remains the same for the next carousel. The selectedItem property assigns 0 only for the first render .

for example: if I was starting from position 0 , then scrolling to the third image at the first user , i will also view the third image of the next user, when the current user changes.

what I try to achieve is , resetting the slider’s position to 0 whenever a user changes. thanks 😃

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
renancleyson-devcommented, Feb 3, 2021

A better workaround to your problem is to use the key prop since it creates a new instance rather than update the current one when the key changes. Your code will be something like this:

const UserDisplay = ({ user, handleLike, handleUnLike }) => {

  const { firstName, images } = user;
      <div>
        <Carousel
          // When it changes, a new instance will be created and the state will reset
          key={user.someUniqueValue} 
          infiniteLoop={true}
          showStatus={false}
          showArrows={false}
          showThumbs={false}
          selectedItem={0}
        >
          {images.map(({ img }, i) => (
            <div className="item" key={i}>
              <ProfilePicture urlPath={img}>
                <Text>{`${firstName}`}</Text>
              </ProfilePicture>
            </div>
          ))}
        </Carousel>
      </div>
};
2reactions
robertosampayocommented, Dec 31, 2020

I had the same problem with react-responsive-carousel days ago, I just did this:

I will use your component as example:

  // first import useRef
  import React, { useRef } from 'react';

  const UserDisplay = ({ user, handleLike, handleUnLike }) => {

  // use useRef to get the carousel instance
  let carousel = useRef(null);

  const { firstName, images } = user;

  // then in a useEffect that listen to user or images for example and waiting for a change, set
  // the valor of slider to 0

  useEffect(() => {
    // some validation to set the slider to 0
     if (carousel && carousel?.state?.selectedItem > 0) {
       carousel.state.selectedItem = 0;
     }
  }, [images]);

  
      <div>
        <Carousel
          ref={el => (carousel = el)} // useRef
          infiniteLoop={true}
          showStatus={false}
          showArrows={false}
          showThumbs={false}
          selectedItem={0}
        >
          {images.map(({ img }, i) => (
            <div className="item" key={i}>
              <ProfilePicture urlPath={img}>
                <Text>{`${firstName}`}</Text>
              </ProfilePicture>
            </div>
          ))}
        </Carousel>
      </div>
 };

I hope this could help you 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to reset the position of the carousel when the button was ...
I'm trying to make the image slider to reset its position when the "Main Hall" to "Parking Space" buttons were clicked.
Read more >
React - How To Reset The Value Of Many Sliders At Once
goTo(slideNumber, dontAnimate), Go to slide index, if dontAnimatetrue, it happens without animation. next(), Change current slide to next slide. prev(), Change.
Read more >
JavaScript - Bootstrap
Use data attributes to easily control the position of the carousel. data-slide accepts the keywords prev or next , which alters the slide...
Read more >
Documentation - Keen-Slider
Documentation. Complete documentation of the installation and usage of Keen-Slider. For the documentation of the old version click here.
Read more >
Carousel | Components - BootstrapVue
When using images in each slide, ensure they all have the same dimensions (or ... Internally, <b-carousel-slide> uses the <b-img> component to render...
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