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

249
Visualizações
React query stuck in infinite loop when adding dynamic time value

I'm trying to add a Date object as a react-query variable, however whenever I use a dynamic date, instead of a static value it calls the query infinitely, instead of just once. I'm not sure of best way to break this loop or to refactor the code so that it will run only once. Can anyone see what I am doing doing wrong?

To be clear, the infinite loop happens when I replace "2022-04-20T23:40:09.038Z" with the 'today' const.

const MoviesPage = () => {
  const [activeTab, setActiveTab] = useState<TabType | undefined>(tabs?.[0]);
  const [noOfColumns, setNoOfColumns] = useState(0);
  const [{ page, titles }, setState] = useState<State>(DEFAULT_STATE);

  const handleTabClick = (tab: TabType) => {
    setState({ page: 0, titles: [] });
    setActiveTab(tab);
  };

  const today = dayjs().toISOString();

  const { data, isFetching } = useMoviesQuery(gqlClient, {
    page: page,
    limit: 16,
    afterDate: activeTab?.name === "coming-soon" && "2022-04-20T23:40:09.038Z",
  });

  useEffect(() => {
    if (isFetching) {
      return;
    }
    if (data) {
      setState((v) => {
        return {
          ...v,
          titles: [...v.titles, ...data.movies],
          page: page,
        };
      });
    }
  }, [data, isFetching, setState, page]);
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Not really sure what useMoviesQuery is doing, but i would change it to the following. Passing in objects like data for example without accessing a property are prone to cause infinite loops(this is entirely based on my experience).

My assumption is that useMoviesQuery is constantly being ran based on a onChange event maybe ? therefore data is constantly being updated triggering the useEffect. Passing in just isFetching should be suffice enough to get the latest updates from data. That is, if isFetching is being updated as well. It's possibly being set from true to false within milliseconds based on the onChangeEvent or however this query is being trigged.

const MoviesPage = () => {
  const [activeTab, setActiveTab] = useState<TabType | undefined>(tabs?.[0]);
  const [noOfColumns, setNoOfColumns] = useState(0);
  const [{ page, titles }, setState] = useState<State>(DEFAULT_STATE);

  const handleTabClick = (tab: TabType) => {
    setState({ page: 0, titles: [] });
    setActiveTab(tab);
  };

  const today = dayjs().toISOString();

  const { data, isFetching } = useMoviesQuery(gqlClient, {
    page: page,
    limit: 16,
    afterDate: activeTab?.name === "coming-soon" && "2022-04-20T23:40:09.038Z",
  });

  useEffect(() => {
    if (isFetching) {
      return;
    }
    if (!isFetching && data) {
      setState((v) => {
        return {
          ...v,
          titles: [...v.titles, ...data.movies],
          page: page,
        };
      });
    }
  }, [isFetching])
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