noLeakedRender
此内容尚不支持你的语言。
Summary
Section titled “Summary”- Rule available since:
v2.3.8 - Diagnostic Category:
lint/nursery/noLeakedRender - This rule doesn’t have a fix.
- The default severity of this rule is information.
- This rule belongs to the following domains:
- Sources:
- Inspired from
react/no-leaked-render
- Inspired from
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noLeakedRender": "error" } } }}Description
Section titled “Description”Prevent problematic leaked values from being rendered.
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
Section titled “Examples”Invalid
Section titled “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.
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
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.