DEV Community

Helder Burato Berto
Helder Burato Berto

Posted on • Edited on • Originally published at helderberto.com

6 4

ReactJS Tips & Tricks: Avoid Short-Circuit Conditional UI Rendering

The goal of this articles is to share with you some insights that I have learned
over the last few years that I have been working with ReactJS.

I will start with a common one that is called Short-Circuit Conditional unexpected UI rendering.

What is a Short-Circuit conditional?

This conditional is a concise way to render UI components.

Example of the Short-Circuit conditional approach:

const Component = ({ number = 0 }) => number && <div>Current: {number}</div>
Enter fullscreen mode Exit fullscreen mode

The component I mentioned before will backfire a 0.

Why does it render zero instead of the empty UI?

The comparison operators in JavaScript don't return boolean values, they return one of the compared values.

In the case mentioned above, when we check the number value it will render zero.

How to avoid the unexpected UI rendering

The way to avoid this issue is using the ternary comparison to be explicit about what will return in both scenarios.

Fixing the Component using the ternary comparison such as:

const Component = ({ number = 0 }) => (number ? <div>Current: {number}</div> : null)
Enter fullscreen mode Exit fullscreen mode

Considering the value of number variable is zero, it will return null that is the second option from the ternary on this case React won't render because it is a null value.

Wrapping Up

If you think this series of articles is helpful to you, or do you want to discuss some programming topics, feel free to reach out to me at @helderburato.

Thanks! ⚡️

Tiugo image

Modular, Fast, and Built for Developers

CKEditor 5 gives you full control over your editing experience. A modular architecture means you get high performance, fewer re-renders and a setup that scales with your needs.

Start now

Top comments (5)

Collapse
 
omarbenmegdoul profile image
Omar Benmegdoul

Just use !!number

Collapse
 
helderberto profile image
Helder Burato Berto • Edited

You can do that, but personally, I think the ternary is more explicit in that case, and you avoid to do a value conversion before render.

Collapse
 
vladi160 profile image
vladi160

I am not sure you can return null for JSX, try with '' or <></>

Collapse
 
multiwebinc profile image
Mike Robinson

Yes @vladi160 , you can return null in JSX.

Collapse
 
maciekgrzybek profile image
Maciek Grzybek

You can 💯 :)

Neon image

Next.js applications: Set up a Neon project in seconds

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started →

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay