lundi 29 juin 2020

Conditional rendering with typescript -

When trying to conditionally render something from a datastructure, I want the element to appear if there exists a record of it in the dataset, but not if it doesnt. My attempt for this was this:

// other code...

{patient.address[0]
    ? patient.address[0].line[0]
    : null}

// other code...

This works for instances where the field is present, but when it isnt - i get a " TypeError: Cannot read property '0' of undefined".

I think the problem is that when trying to access .address[0], the error gets thrown due to the array not existing on the dataset, before it can enter the conditional. How can I ammend my approach to fix this?




Aucun commentaire:

Enregistrer un commentaire