kentcdodds / ama

Ask me anything!
https://github.com/kentcdodds/ama/issues?q=is%3Aissue+is%3Aclosed
685 stars 75 forks source link

Null vs Empty fragment? #822

Closed karangarg45 closed 4 years ago

karangarg45 commented 4 years ago

Hi Kent,

In react if you want to render nothing either you can return null from the component or you can render an empty fragment.

Is there any disadvantage of using <React.Fragment/> over null.What are your thoughts on this? I think using React.Fragment is more explicit and shows developer's intent

kentcdodds commented 4 years ago

Hi @karangarg45,

I don't think there's any real observable difference here. Given that it's all about your preference and for me, I prefer to render null when I want nothing rendered. Fragments are intended to encapsulate children and if I saw an empty fragment I would be confused why it's there 🤷‍♂️