Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check object in React for null or empty or undefined?

I have the following sequence of code in React:

{financialPerformance && (
  financialPerformance.isConfirmed ? (
    <L.Text txtGray>Confirmed</L.Text>
  ) : (
    <L.Text txtGray>Not Confirmed</L.Text>
  )
)}

I have to check financialPerformance itself as well for null or empty or undefined and display "Non Confirmed" message. I mean on first appearance of financialPerformance object.

{financialPerformance && (

How can I do that inside or out of block above?

like image 928
tesicg Avatar asked Mar 16 '26 07:03

tesicg


1 Answers

Due to null and undefined will be evaluated to false in boolean context - you can just combine your checks at one place:

{
  financialPerformance && financialPerformance.isConfirmed ? (
    <L.Text txtGray>Confirmed</L.Text>
  ) : (
    <L.Text txtGray>Not Confirmed</L.Text>
  )
}
like image 71
falinsky Avatar answered Mar 18 '26 19:03

falinsky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!