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) || ""
}";
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