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

128
Visualizações
Checking variable outside of its scope

In my code I want to execute a query conditionally and then check its loading variable inside the template, but only if the query is executed and don't want to have any TypeScript errors in my file. How do I check this variable which might or might not exist in its context?

I am using Apollo GraphQL.

if( executeMyQuery )
{
    const {
        data, loading: loadingQuery
    } = useQuery( MyQuery );
}

return (
    <View>
        { /* ... some code ... */ }
        { executeMyQuery && loadingQuery ?
            <Text>Query is loading...</Text>
            { /* ... some code ... */ }
        : null }
    </View>
);

TypeScript says: Cannot find name 'loadingQuery'.

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

0

Take a look at dependent queries

const {
     data, loading: loadingQuery
} = useQuery( MyQuery,    {
 // The query will not execute until the userId exists
 enabled: executeMyQuery,
   }
);

return (
    <View>
        { /* ... some code ... */ }
        { executeMyQuery && loadingQuery ?
            <Text>Query is loading...</Text>
            { /* ... some code ... */ }
        : null }
    </View>
);
about 4 years ago · Juan Pablo Isaza Relatório

0

Fixed it using the skip parameter by changing

if( executeMyQuery )
{
    const {
        data, loading: loadingQuery
    } = useQuery( MyQuery );
}

To

const {
    data, loading: loadingQuery
} = useQuery( MyQuery, {
    skip: executeMyQuery,
} );
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