Skip to content

noLeakedRender

¥Summary

¥How to configure

biome.json
{
"linter": {
"rules": {
"nursery": {
"noLeakedRender": "error"
}
}
}
}

¥Description

防止渲染有问题的泄漏值。

¥Prevent problematic leaked values from being rendered.

此规则禁止使用可能导致 React JSX 中意外渲染值或渲染崩溃的值。使用逻辑 AND 运算符 (&&) 进行条件渲染时,如果左侧计算结果为假值(例如 0NaN 或任何空字符串),则会渲染这些值,而不是不渲染任何内容。

¥This rule prevents values that might cause unintentionally rendered values or rendering crashes in React JSX. When using conditional rendering with the logical AND operator (&&), if the left-hand side evaluates to a falsy value like 0, NaN, or any empty string, these values will be rendered instead of rendering nothing.

¥Examples

¥Invalid

const Component = () => {
const count = 0;
return <div>{count && <span>Count: {count}</span>}</div>;
}
code-block.jsx:3:16 lint/nursery/noLeakedRender ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Potential leaked value that might cause unintended rendering.

1 │ const Component = () => {
2 │ const count = 0;
> 3 │ return <div>{count && <span>Count: {count}</span>}</div>;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4 │ }
5 │

JavaScript’s && operator returns the left value when it’s falsy (e.g., 0, NaN, ”). React will render that value, causing unexpected UI output.

Make sure the condition is explicitly boolean.Use !!value, value > 0, or a ternary expression.

const Component = () => {
const items = [];
return <div>{items.length && <List items={items} />}</div>;
}
code-block.jsx:3:16 lint/nursery/noLeakedRender ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Potential leaked value that might cause unintended rendering.

1 │ const Component = () => {
2 │ const items = [];
> 3 │ return <div>{items.length && <List items={items} />}</div>;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4 │ }
5 │

JavaScript’s && operator returns the left value when it’s falsy (e.g., 0, NaN, ”). React will render that value, causing unexpected UI output.

Make sure the condition is explicitly boolean.Use !!value, value > 0, or a ternary expression.

const Component = () => {
const user = null;
return <div>{user && <Profile user={user} />}</div>;
}
code-block.jsx:3:16 lint/nursery/noLeakedRender ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Potential leaked value that might cause unintended rendering.

1 │ const Component = () => {
2 │ const user = null;
> 3 │ return <div>{user && <Profile user={user} />}</div>;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4 │ }
5 │

JavaScript’s && operator returns the left value when it’s falsy (e.g., 0, NaN, ”). React will render that value, causing unexpected UI output.

Make sure the condition is explicitly boolean.Use !!value, value > 0, or a ternary expression.

¥Valid

const Component = () => {
const count = 0;
return <div>{count > 0 && <span>Count: {count}</span>}</div>;
}
const Component = () => {
const items = [];
return <div>{!!items.length && <List items={items} />}</div>;
}
const Component = () => {
const user = null;
return <div>{user ? <Profile user={user} /> : null}</div>;
}
const Component = () => {
const condition = false;
return <div>{condition ? <Content /> : <Fallback />}</div>;
}
const Component = () => {
const isReady = true;
return <div>{isReady && <Content />}</div>;
}

¥Related links