Rules
no-leaked-conditional-rendering
Full Name in eslint-plugin-react-x
Full Name in @eslint-react/eslint-plugin
Features
💭
Presets
recommended-type-checked
Description
Prevents bugs caused by rendering values like 0, NaN, or "" in JSX when using the && operator. These values are falsy in JavaScript, but React may still attempt to render them, leading to:
- Unexpected UI output: For example,
{0 && <div />}renders0in the DOM instead of nothing. - Crashes in React Native: Rendering
NaN,0, or""can cause runtime crashes in versions before React 18.
Examples
In React, you might end up rendering unexpected values like 0 or NaN. In React Native, your render method will even crash if you render these values:
This can be avoided by:
- Coerce the value to a boolean:
{!!someValue && <Component />} - Use a ternary:
{someValue ? <Component /> : null} - Use comparisons:
{someValue > 0 && <Component />}
Failing
Passing
Implementation
Further Reading
See Also
no-complex-conditional-rendering
Warns when conditional rendering is too complex.