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

208
Visualizações
"Uncaught Error: Too many re-renders. React limits the number of renders to prevent an infinite loop."

I came across this error int he console. I had a look through the previous questions but they were not much help. Anyone know how I have caused this infinite loop?

"Uncaught Error: Too many re-renders. React limits the number of renders to prevent an infinite loop."

import logo from './logo.svg';
import './App.css';
import Movie from './components/Movie'
import React, { useEffect, useState } from 'react';

  const FEATURED_API = "https://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc&api_key=04c35731a5ee918f014970082a0088b1&page=1"
  const IMG_API = "https://image.tmdb.org/t/p/w1280"
  const SEARCH_API = "https://api.themoviedb.org/3/search/movie?&api_key=04c35731a5ee918f014970082a0088b1&query="


  function App() {
    const [movies, setMovies] = useState([]);

    useEffect(() => {
      fetch(FEATURED_API)
      .then((res) => res.json())
      .then((data) =>  {
        console.log(data)
        setMovies(data.results);
      });
    },[])
    setMovies(movies)
  

    return <div>{ movies.length > 0 && movies.map((movie) =>  <Movie/>)}</div>
    
}
export default App;

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can't set states outside functions or hooks in components.

Just remove the setMovies(movies) and it should work ;)

about 4 years ago · Juan Pablo Isaza Relatório

0

Keep in mind that the setMovies function queues a re-render that will take place as soon as the component has finished rendering. Given this behavior, if the setMovies function is placed outside of a function or a useEffect, it will get you stuck in a loop!

about 4 years ago · Juan Pablo Isaza Relatório

0

The problem is from calling setMovies(movies) inside root of App function and each time App component must to re-render, that's will execute and you will stuck in infinite loop, so you can replace your code with below:

import logo from './logo.svg';
import './App.css';
import Movie from './components/Movie'
import React, { useEffect, useState } from 'react';

  const FEATURED_API = "https://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc&api_key=04c35731a5ee918f014970082a0088b1&page=1"
  const IMG_API = "https://image.tmdb.org/t/p/w1280"
  const SEARCH_API = "https://api.themoviedb.org/3/search/movie?&api_key=04c35731a5ee918f014970082a0088b1&query="


  function App() {
    const [movies, setMovies] = useState([]);

    useEffect(() => {
      fetch(FEATURED_API)
      .then((res) => res.json())
      .then((data) =>  {
        console.log(data)
        setMovies(data.results);
      });
    },[]);
  
    return <div>{ movies.length > 0 && movies.map((movie) =>  <Movie/>)}</div>
    
}
export default App;
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