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

114
Visualizações
Infinite loop of useEffect with empty dependency array

code running correctly while in fulfilled case of extraReducer isLoading's state is not used SearchForm component is not written fully, just what is needed to understand the issue

const fetchMovies = createAsyncThunk(
  'movies/fetchMovies',
  async (movieTitle) => {
    const newMovies = await searchApi.findMovie(movieTitle)
    return newMovies
  }
)

extraReducers: (builder) => {
    builder
      .addCase(fetchMovies.pending, (state, action) => {
        state.isLoading = true
      })
      .addCase(fetchMovies.fulfilled, (state, action) => {
        state.isLoading = false // running an infinite loop
        }
      })


import { fetchMovies } from '../../redux/slices.js/moviesSlice'
export function SearchForm() {
  const dispatch = useDispatch()

  // movies default value
  useEffect( () => {
    dispatch(fetchMovies('Titanic'))
  }, [])
  

  return (
    <form>
      <input></input>
      <button></button>
    </form>
  )
}

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

0

Your code is still very incomplete. I have no idea why you're using useEffect to fetch the movies. Because of the way your SearchForm component is laid out, I guess that the user searches for a movie title, submits and then the action of fetching the movies based on the title gets dispatched.

So in that scenario, using useEffect is not appropriate and you have to use a function to handle the submit.

import { useState } from "React"

const SearchForm = () => {

const [input, setInput] = useState("");

const handleSubmit = (e) => {
  e.preventDefault();
  if (!input) return;

  dispatch(fetchMovies(input));
}

  return (
    <form onSubmit={handleSubmit}>
      <input value={input} onChange={(e) => setInput(e.target.value)}></input>
      <button type="submit">Search</button>
    </form>
  )
}
about 4 years ago · Juan Pablo Isaza Relatório

0

it turns out cause I'm trying to render conditionally as follow:

export default function HomePage() {
  return(
    {isLoading 
    ? <Preloader />
    : <Routing />
    }
  )
}

as I guess it isn't correct to render this way

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