DEV Community

Discussion on: 4 Ways to Render JSX Conditionally in React

Collapse
 
k1dv5 profile image
Kidus Adugna

Why not just return instead of return null?

Collapse
 
danwalsh profile image
Dan Walsh

If I remember correctly, return on its own infers a return type of void, whereas return null is an acceptable React.ReactNode return type, as far as TypeScript is concerned.

Thread Thread
 
fjones profile image
FJones

And void in turn results in undefined on the caller, which React now supports, but I would still discourage. The reason support for it was added was, in part, inconsistency between nested and root-level conditional rendering.

Collapse
 
rasaf_ibrahim profile image
Rasaf Ibrahim

Yes, you can definitely do that as we know whenever JavaScript sees the return keyword, it immediately exits the function.