Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

124
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda