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

97
Visualizações
React Native not displaying correct image

I'm trying to make a slideshow where the user can play them in a default or random order and cycle through them by pressing a button. The problem comes when the user has chosen random mode; pressing the button still increments the currentIndex variable, but for some reason images[currentIndex] does not change.

const SlideshowScreen = (props) => {
const settings = props.route.params.settings;
const [images, setImages] = useState(settings.random ? props.route.params.images.sort((a, b) => 0.5 - Math.random()) : props.route.params.images);

const [currentIndex, setCurrentIndex] = useState(0);

useEffect(() => {
    console.log('currentIndex', currentIndex, images[currentIndex]);
}, [currentIndex])

const newImage = () => {
    if (currentIndex + 1 >= images.length) {
        console.log('done');
    }
    else {
        setCurrentIndex(currentIndex + 1);
    }
}

return (
    <View style={styles.mainContainer}>
        <View style={styles.imageContainer}>
            <Image
                style={styles.image}
                source={images[currentIndex].path} key={images[currentIndex].id} />
        </View>
        <TouchableOpacity onPress={() => newImage()}>
                    <Text>Next image</Text>
                </TouchableOpacity>
    </View>

)
}

On some clicks of the button this happens, and on others it doesn't. How is this happening? Any help would be great, thanks.

about 4 years ago · Juan Pablo Isaza
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