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

344
Visualizações
React, useEfect and axios. infinite loop

I am trying to get data from an api, The Movie Database, for which I am using React (version 18) and axios (version 0.27.2). The question is that when I take the data, and the setState is done. starts an infinite loop that doesn't end. I share code for better understanding.

import { useEffect, useState } from "react"
import { useNavigate } from "react-router-dom"
import axios from 'axios'


function Listado() {

    const [movieList, setMovieList] = useState([])

    const navigate = useNavigate()

    useEffect(() => {

        const token = localStorage.getItem('token')

        console.log(token)
        if (token == null) {
            navigate('/')
        }
    }, [navigate])

   
    useEffect(() => {

        const api = 'b34999ac538aed71a19355d996f1081f'
        const options = {
            method: 'GET'
        }

        const rest = (async () => {
            await axios(`https://api.themoviedb.org/3/discover/movie?api_key=${api}&language=es-ES&page=1p`, options)
                .then(response => {
                    setMovieList(response.data.results)
                    console.log(movieList)
                })
        })

        rest()

    }, [movieList])
   

    return (
        <>
            
            <div className='col3' style={{ border: '1px solid red' }}>Peli 1</div>

        </>)
}
export default Listado

The result it gives is the following in the console:

(twenty) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {… }, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]

Line that does not stop repeating itself infinitely. I hope there is someone who can shed light on this problem that I don't know how to solve. Greetings and 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