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

getCurrentIndex returns wrong value when data count is < 3 #299

Closed
jb-5murf opened this issue Nov 2, 2022 · 8 comments
Closed

getCurrentIndex returns wrong value when data count is < 3 #299

jb-5murf opened this issue Nov 2, 2022 · 8 comments
Assignees
Labels
bug Something isn't working needs review The issue has been solved, Wait for confirmation. P1

Comments

@jb-5murf
Copy link

jb-5murf commented Nov 2, 2022

Describe the bug
In my project I have map with markers and carousel with detail of selected marker. Swiping the carousel moves the map to the next (previous) marker. When I use onSnapToItem to set new index it's quite slow because the event is fired after the swipe is fully completed. So I decided to set the current index using getCurrentIndex in onProgressChange. It works great if data count is 3 or more. But when looping carousel with 2 items I get indexes 0, 1, 2, 3, 0, 1, 2, 3... instead of 0, 1, 0, 1, 0, 1...

To Reproduce

import React, { useRef } from 'react';
import { View, Dimensions } from 'react-native';
import Carousel from 'react-native-reanimated-carousel';

const colors = ['red', 'green'];

export default function App() {
    const carouselRef = useRef();
    return (
        <Carousel
            ref={carouselRef}
            data={colors}
            renderItem={({ item }) => <View style={{ flex: 1, backgroundColor: item }} />}
            width={Dimensions.get('window').width}
            onProgressChange={(offsetProgress, absoluteProgress) => {
                const index = carouselRef?.current?.getCurrentIndex();
                console.log(index);
            }}
        />
    );
}

Expected behavior
Get right current index.

Versions (please complete the following information):

  • react: v18.0.0
  • react-native: v0.69.6
  • react-native-reanimated: v2.9.1
  • react-native-reanimated-carousel: v3.1.3
  • react-native-gesture-handler: v2.5.0
@jb-5murf jb-5murf added the bug Something isn't working label Nov 2, 2022
@dohooo
Copy link
Owner

dohooo commented Nov 2, 2022

#268
image

@dohooo dohooo closed this as completed Nov 2, 2022
@dohooo dohooo reopened this Nov 2, 2022
@dohooo dohooo added the needs review The issue has been solved, Wait for confirmation. label Nov 2, 2022
@jb-5murf
Copy link
Author

jb-5murf commented Nov 3, 2022

I think it is quite similar but not exactly the same. onSnapToItem returns right index. Calling getCurrentIndex in onProgressChange returns wrong index.

@dohooo dohooo added P1 and removed needs review The issue has been solved, Wait for confirmation. labels Nov 3, 2022
@dohooo dohooo closed this as completed in 7f94027 Nov 4, 2022
@dohooo
Copy link
Owner

dohooo commented Nov 4, 2022

3.1.5

@dohooo dohooo added the needs review The issue has been solved, Wait for confirmation. label Nov 4, 2022
@jb-5murf
Copy link
Author

jb-5murf commented Nov 4, 2022

Hi, I still get wrong indexes in 3.1.5

@dohooo
Copy link
Owner

dohooo commented Nov 4, 2022

Hi, I still get wrong indexes in 3.1.5

What!?.. Did you clear the cache? Just like

expo start -c

@dohooo
Copy link
Owner

dohooo commented Nov 4, 2022

Hi, I still get wrong indexes in 3.1.5

And also you need to add autoFillData={false} prop when the data length is less than 3.

@jb-5murf
Copy link
Author

jb-5murf commented Nov 7, 2022

And also you need to add autoFillData={false} prop when the data length is less than 3.

This helped 👍 Thank you!

@mrigo
Copy link

mrigo commented Feb 8, 2024

Hey, version 3.5.1, same issue with <= 3 items :/
autoFillData={false} not helping..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs review The issue has been solved, Wait for confirmation. P1
Projects
None yet
Development

No branches or pull requests

3 participants