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

173
Visualizações
UseQuery Async Variables Issue

I am trying to make a simple useQuery request but the variables which I am dependent on for the request are being loaded asynchronously. My apologies in advance for the kind of messy variable names.

Currently I have a useQuery hook which I want to function like this.

 const { id } = router.query;

 let userDbAddress;

  useEffect(() => {
    async function getUsernameEthAddress() {
      userDbAddress = await getUserAddressFromUsernameDb(id);
    }
    getUsernameEthAddress();
    return () => (mountedRef.current = false);
  }, [router.isReady]);

  let userVariables = {
    where: {
      id: userDbAddress,
    },
  };

  const { loading, error, data } = useQuery(GET_USER_DATA, {
    variables: userVariables,   //<<<I AM NULL (because running before async useEffect completes!)
  });
  if (loading) return null;
  if (error) return <Error statusCode={404} />;

  const myData = data?.myData;

My problem is that the asynchronous is in the ordering of my data. The userDbAddress is null when the useQuery is called and I am not sure when I can best pass it into the variables parameter for my query. I am not sure if this is the proper use case for useLazyQuery? I want my query to run on page render just after the useEffect hook runs so that I can wire up the userDbAddress variable to pass into the useQuery hook.

Thanks for taking the time:)

And happy to clarify if the question is too messy/wordy

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

0

This is react-query right?

If so, there is a 3rd parameter to set options for the query. Just add { enabled: false } as an option in that 3rd parameter and the query won't fire off immediately. Secondly, pull the refetch function from the object returned from useQuery(). Then, once userVariables has valid data (you can create a useEffect() for this), you can call refetch() and your expected flow work work now.

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