1

I am using the below statement to conditionally render a component.

{data?.length && <RenderThisComp />}

data - an array.

RenderThisComp - a component that needs to be rendered only if data array has any value.

If the length of data array is zero, it is showing zero, whereas it should display nothing.

  • `{data.length && }` – Sabbin Apr 14 '20 at 06:55
  • Optional chaining is a stage 4 proposal, you might want to enable those in your webpack config. – Utsav Patel Apr 14 '20 at 06:56
  • Personally, I would just use `(data || []).length && < ... >`. – Sulthan Apr 14 '20 at 06:57
  • Given your setup supports optional chaining, `{data?.length > 0 && }` should work. – str Apr 14 '20 at 07:03
  • Optional chaining is not a problem here. I have enabled it from webpack config. Thanks, everyone for replying. I got an answer from here - https://stackoverflow.com/questions/53048037/react-showing-0-instead-of-nothing-with-short-circuit-conditional-component – aakanksha_02 Apr 14 '20 at 07:26

0 Answers0