Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

213
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda