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

handle currentIndex inside renderItem #604

Open
ghost opened this issue Oct 22, 2019 · 3 comments
Open

handle currentIndex inside renderItem #604

ghost opened this issue Oct 22, 2019 · 3 comments

Comments

@ghost
Copy link

ghost commented Oct 22, 2019

Question

I need to access to currentIndex inside renderItem to know if the current displayed item isFocus.

For the moment, I handle the index change with onSnapToItem with :

  const [carouselIndex, setIndex] = React.useState(initialIndex);
  const refCarousel = React.useRef(null);

  const handleIndexChange = (index: number) => {
    setIndex(index);
  };

 <Carousel
        data={productList}
        ref={refCarousel}
        style={fillContainer}
        sliderWidth={width}
        itemWidth={width}
        firstItem={initialIndex}
        onBeforeSnapToItem={handleIndexChange}
        keyboardShouldPersistTaps="always"
        renderItem={({ item, index }) => {
          return (
            <InventoryProductSheet
              key={item.id}
              productId={item.id}
              index={index}
              isFocused={index === carouselIndex}

            />
          );
        }}
      />

But there is some bug with hooks and react-native-snap-carousel (#559)

i have tried refCarousel.current.currentIndex but its doesn't trigger rerender :/

There is a better way to handle currentIndex inside renderItem ?

@mohmdalfaha
Copy link

mohmdalfaha commented Nov 21, 2019

@ludwig-adoria Have you solved this issue yet ?? I am facing the same

Update

I've just used onSnapToIndex={(index) => setIndex(index)}

@dohooo

This comment was marked as spam.

@umair-khanzada
Copy link

I also need to access the currentIndex I get renderItem but it's not changing on the very first change to forward/scrolldown after that it starts working but always shows activeIndex - 1, which works perfectly with back/scollup.

snapCarouselActiveIndexIssue.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants