meliorence / react-native-snap-carousel

Swiper/carousel component for React Native featuring previews, multiple layouts, parallax images, performant handling of huge numbers of items, and more. Compatible with Android & iOS.
BSD 3-Clause "New" or "Revised" License
10.31k stars 2.28k forks source link

handle currentIndex inside renderItem #604

Open ghost opened 4 years ago

ghost commented 4 years ago

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 (https://github.com/archriss/react-native-snap-carousel/issues/559)

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

There is a better way to handle currentIndex inside renderItem ?

mohmdalfaha commented 4 years ago

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

Update

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

dohooo commented 2 years ago

Sorry, please allow me to advertise for my open source library! ~ I think this library react-native-reanimated-carousel will solve your problem. It is a high performance and very simple component, complete with React-Native reanimated 2

umair-khanzada commented 2 months ago

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.

https://github.com/meliorence/react-native-snap-carousel/assets/8782817/85ea9490-2e35-451d-99e7-9c359e8ff8f8