Let's talk about something we all know when developing our project using React: Conditional Rendering.
When it comes to Conditional Rendering in React, there are multiple ways to implement it. One of the favorite ways among developers is using && operator.
const Text = (isDisplayed) => {
return (
<div>
// this one
{isDisplayed && <p>{text}</p>}
</div>
);
};
Basically, if the condition is true, a HTML <p>
element with a specific text will be displayed. Otherwise, it won't be rendered at all.
But have you ever checked all the possible values that can be true and false? If not, let's check out the results below!
// Truthy values
// boolean
{true && <p>πΈ</p>}
// number
{20 && <p>πΈ</p>}
// string
{"string" && <p>πΈ</p>}
// object
{{} && <p>πΈ</p>}
// array
{[] && <p>πΈ</p>}
// function
{(() => {}) && <p>πΈ</p>}
// symbol
{Symbol("symbol") && <p>πΈ</p>}
// All of them will render <p>πΈ</p> β
So far, we haven't had to worry much about truthy conditions. But now, have you ever thought about what happens when the condition is falsy?
// Falsy values
// null - this won't render anything β
{null && <p>πΈ</p>}
// undefined - this won't render anything β
{undefined && <p>πΈ</p>}
// boolean - this won't render anything β
{false && <p>πΈ</p>}
// NaN - this will render NaN π€― β
{NaN && <p>πΈ</p>}
// 0 - this will render 0 π€― β
{0 && <p>πΈ</p>}
// negative 0 - this will render 0 π€― β
{-0 && <p>πΈ</p>}
// string - this won't render anything β
{"" && <p>πΈ</p>}
As it turns out, not all falsy values give the expected result. It can be quite baffling! π€― This issue often arises when dealing with a Notification Badge
.
You might think it's a minor problem, but guess what? Many developers simply forget to deal with it the right way!
Solutions
Luckily, I've got a handful of solutions for this issue.
1. Convert condition
to Boolean
// NaN
{Boolean(NaN) && <p>πΈ</p>}
// 0
{Boolean(0) && <p>πΈ</p>}
// negative 0
{!!-0 && <p>πΈ</p>}
// string
{!!"" && <p>πΈ</p>}
// All of them won't render anything β
You can handle this by using the Boolean(expression)
function or the double NOT operator !!expression
. These methods will convert any data type into a boolean value. As seen in the results above, both true
and false
work perfectly fine in this case.
2. Use Ternary operator
// NaN
{NaN ? <p>πΈ</p> : null}
// 0
{0 ? <p>πΈ</p> : null}
// negative 0
{-0 ? <p>πΈ</p> : null}
// string
{"" ? <p>πΈ</p> : null}
// All of them won't render anything β
When the condition is falsy
, you can use the ternary operator to return null
instead.
Conclusion
So, we'll save ourselves from headaches and level up the quality of our code if we keep these things in mind. I hope this post has provided useful insights for your projects! β¨
Bonus
If you already have eslint-plugin-react installed, you can enable the following rule.
{
// ...
"react/jsx-no-leaked-render": ["error", { "validStrategies": ["coerce"] }]
// ...
}
By doing so, ESLint will automatically replace all your Conditional Rendering codes to boolean.
// From this β
{NaN && <p>πΈ</p>}
// To this β
{!!NaN && <p>πΈ</p>}
Top comments (6)
Love this. Too many times I have come across a leaked render. Definitely worth having a lint rule for
Yeah, it does. You can increase your productivity by letting ESLint take care of this issue and focus on other things.
Good one
Nice article, although the title is a bit of misleading
Great
great post, I think adding
Boolean(expression)
or using ternary operator is solving anything