meliorence / meliorence/react-native-snap-carousel

handle currentIndex inside renderItem

Open
#604 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
10.5k
Forks
2.3k
PR merge metrics
No merged PRs in 30d

Description

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 ?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the Carousel renderItem callback, onBeforeSnapToItem, and the refCarousel.current.currentIndex behavior described in the issue. Determine whether the component can expose a rerender-safe current index to renderItem, and define done as reliably identifying the focused item without the reported hooks issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react-native
Domain
mobile
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.