Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switching items Carousel still stores last view index problem #479

Open
MariuzM opened this issue Oct 13, 2023 · 4 comments
Open

Switching items Carousel still stores last view index problem #479

MariuzM opened this issue Oct 13, 2023 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@MariuzM
Copy link

MariuzM commented Oct 13, 2023

Describe the bug
Carousel is storing last view index in its memory, even if i pass new items to it that memory is not cleared. I have issue when i pass more items like 6 then scroll to its last index and then when i pass 2 items to carousel it still stays ad that 6th index.

Expected behavior
When passing new items to carousel the memory of its last index view should clear and always start from 0

Video example

Screen.Recording.2023-10-13.at.10.13.58.mov

Versions (please complete the following information):
"react": "18.2.0",
"react-native": "0.72.5",
"react-native-reanimated": "~3.3.0",
"react-native-reanimated-carousel": "^3.5.1",
"react-native-gesture-handler": "~2.12.0",

Repo: https://github.com/MariuzM/bun-and-expo/tree/carousel

@MariuzM MariuzM added the bug Something isn't working label Oct 13, 2023
@MariuzM
Copy link
Author

MariuzM commented Oct 13, 2023

Partly i was able to solve this by

  const ref = useRef<ICarouselInstance>(null);

  useEffect(() => {
    ref.current?.scrollTo({ index: 0 });
  }, [p.items]);

But if i still scroll to index 3 and then chose items with only 2 index it will show blank, again memory issue that its not resenting the index between switching

@MariuzM
Copy link
Author

MariuzM commented Oct 13, 2023

Setting Loop to true solves above but then i end up with using loop which i dont want

@iamdanylo
Copy link

Same problem

@YannickLanteri
Copy link

@MariuzM @iamdanylo
Instead of passing the prop directly to the Carousel, you can go through a state to trigger the update of the data and then perform the scrollTo.
It works with loop={false}.

  const [data, setData] = useState(dataProps);

  useEffect(() => {
    setData(dataProps)

    carouselRef.current?.scrollTo({ index: 0 });
  }, [dataProps]);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants