Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

212
Views
React Native Carousel UseState

I am currently implementing a carousel library (react-native-reanimated-carousel). The code for the following is represented as such:

<Carousel
      width={cardWidth}
      height={cardHeight}
      data={cards}
      onScrollEnd={() => {console.log('ended')}}
      renderItem={({item}) => 
      (
        <View>
          <Image 
          source={{uri: item["ImgURL"],}}
          style={styles.card}
          />
        </View>
      )}
    />

Upon the carousel changing, the item value in the renderItem property changes. Is there a way to get the value of item from outside this element? I want other elements outside to change depending on the value (and properties) of item.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You could try to call a callback function in the renderItem function.

() => {
  const [activeItem, setActiveItem] = useState(cards[0].item);

  return (<Carousel
    width={cardWidth}
    height={cardHeight}
    data={cards}
    onScrollEnd={() => {console.log('ended')}}
    renderItem={({item}) => {

      setActiveItem(item);

      return (
        <View>
          <Image 
          source={{uri: item["ImgURL"],}}
          style={styles.card}
          />
        </View>
      );
    }}
    />)
  );
};

Or you could use onScrollEnd. It provides the previous index and the current index according to the documentation

() => {
  const [activeItem, setActiveItem] = useState(cards[0].item);

  return (<Carousel
    width={cardWidth}
    height={cardHeight}
    data={cards}
    onScrollEnd={(previous, current) => {
      setActiveItem(cards[current].item);
      console.log('ended')
    }}
    renderItem={({item}) => (
        <View>
          <Image 
          source={{uri: item["ImgURL"],}}
          style={styles.card}
          />
        </View>
    )}
    />)
  );
};

Or you could use the Ref prop getCurrentIndex

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!