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

336
Vistas
How to destructure nested data returned from useQuery?

I have an Apollo query:

const { error, loading, data: user } = useQuery(resolvers.queries.ReturnUser, {
    variables: { userId: parseInt(id) },
  });

The data object returned from the query has another object called returnUser. So the actual object is:

data: {
  returnUser: {
    name: 'Some Name'
  }
}

In my JSX I want to output the data:

return (
  <div>
    { user ?
      <p>Profile {user.returnUser.name}</p> :
      <div>User not found</div> }
  </div>
);

As you can see I need to access the returnUser object on the user object to get the actual user data. This is not great. Is there any way to destructure the data object from the useQuery so I can assign the nested object to user?

//edit: While this looks like a duplicate question, the answer is different due to the async nature of useQuery. If you don't set a default value you'll get an error.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Got the answer thanks to @Pilchard:

const {error, loading, data: {returnUser: user} = {}} = useQuery(resolvers.queries.ReturnUser, {
    variables: {userId: parseInt(id)},
  });
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can follow the same nesting while deconstructing the data.

e.g.

const data = {
  anotherLevel: {
     returnUser: {
      name: 'Some Name'
    } 
  }
}

const { anotherLevel: {returnUser: { name }}} = data;

console.log(name); // prints Some Name

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