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.

5
  • {data.length && <RenderThisComp/>} Commented Apr 14, 2020 at 6:55
  • Optional chaining is a stage 4 proposal, you might want to enable those in your webpack config. Commented Apr 14, 2020 at 6:56
  • Personally, I would just use (data || []).length && < ... >. Commented Apr 14, 2020 at 6:57
  • Given your setup supports optional chaining, {data?.length > 0 && <RenderThisComp />} should work. Commented Apr 14, 2020 at 7: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 - stackoverflow.com/questions/53048037/… Commented Apr 14, 2020 at 7:26

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.