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

101
Vistas
check for null when destructuring data with useSelector

How can I check for null when destructuring my data:

   const {
      vehicles: {
         data: { reminderVehicles },
      },
   } = useSelector((state) => state);

The below code gives me

null is not an object, evaluating vehicles.data.reminderVehicles

   document.getElementById("VehicleRegistration").value = "${
        (prepopVehicleReg && reminderVehicles[0]?.Registration) || ""
     }";
about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can always assign a default value to it.

const myData = {
  one: 1,
  two: 2,
  nested: {
    three: 1
  }
};

const {one = null} = myData;
const {five = null} = myData;
const {nested: {three}} = myData;
const {nested: {six = 'Default'}} = myData;


console.log({
 one, 
 three,
 five, 
 six
});

In your case, it should be (assuming reminderVehicles is an array)

const {
    vehicles: {
        data: {
            reminderVehicles
        } = {
            reminderVehicles: []
        }
    } = {}
} = useSelector((state) => state);

But this is not readable and looks very complicated

about 4 years ago · Santiago Trujillo 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