Back to Biomejs

noJsxLiterals

src/content/docs/linter/rules/no-jsx-literals.mdx

latest11.8 KB
Original Source

import { Tabs, TabItem } from '@astrojs/starlight/components';

<Tabs> <TabItem label="JSX and TSX" icon="seti:javascript"> ## Summary - Rule available since: `v2.2.4` - Diagnostic Category: [`lint/style/noJsxLiterals`](/reference/diagnostics#diagnostic-category) - This rule isn't recommended, so you need to enable it. - This rule doesn't have a fix. - The default severity of this rule is [**information**](/reference/diagnostics#information). - Sources: - Same as [`react/jsx-no-literals`](https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-no-literals.md)

How to configure

json
{
	"linter": {
		"rules": {
			"style": {
				"noJsxLiterals": "error"
			}
		}
	}
}

Description

Disallow string literals inside JSX elements.

This rule discourages the use of string literals directly within JSX elements. String literals in JSX can make code harder to maintain, especially in applications that require internationalization or dynamic content.

Examples

Invalid

jsx
<div>Hello World</div>
<pre class="language-text"><code class="language-text">code-block.jsx:1:6 <a href="https://biomejs.dev/linter/rules/no-jsx-literals">lint/style/noJsxLiterals</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Incorrect use of string literal detected.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>&lt;div&gt;Hello World&lt;/div&gt; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">String literals in JSX can make code harder to maintain and internationalize.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Consider avoiding hardcoded strings entirely.</span> </code></pre>
jsx
<>Welcome to our site</>
<pre class="language-text"><code class="language-text">code-block.jsx:1:3 <a href="https://biomejs.dev/linter/rules/no-jsx-literals">lint/style/noJsxLiterals</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Incorrect use of string literal detected.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>&lt;&gt;Welcome to our site&lt;/&gt; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">String literals in JSX can make code harder to maintain and internationalize.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Consider avoiding hardcoded strings entirely.</span> </code></pre>
jsx
<span>
  Please enter your name
</span>
<pre class="language-text"><code class="language-text">code-block.jsx:1:7 <a href="https://biomejs.dev/linter/rules/no-jsx-literals">lint/style/noJsxLiterals</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Incorrect use of string literal detected.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>&lt;span&gt; <strong> │ </strong> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>2 │ </strong> Please enter your name <strong><span style="color: Tomato;">&gt;</span></strong> <strong>3 │ </strong>&lt;/span&gt; <strong> │ </strong> <strong>4 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">String literals in JSX can make code harder to maintain and internationalize.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Consider avoiding hardcoded strings entirely.</span> </code></pre>

Valid

jsx
<div>{'Hello World'}</div>
jsx
<>{'Welcome to our site'}</>
jsx
<span>
  {'Please enter your name'}
</span>
jsx
<div>{`Hello ${name}`}</div>

Options

noStrings

When enabled, the rule will also flag string literals inside JSX expressions and attributes.

Default: false

json
{
	"linter": {
		"rules": {
			"style": {
				"noJsxLiterals": {
					"options": {
						"noStrings": true
					}
				}
			}
		}
	}
}

jsx
<span>
  {'Please enter your name'}
</span>
<pre class="language-text"><code class="language-text">code-block.jsx:2:4 <a href="https://biomejs.dev/linter/rules/no-jsx-literals">lint/style/noJsxLiterals</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Incorrect use of string literal detected.</span> <strong>1 │ </strong>&lt;span&gt; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>2 │ </strong> &#123;'Please enter your name'&#125; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>3 │ </strong>&lt;/span&gt; <strong>4 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">String literals in JSX can make code harder to maintain and internationalize.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Consider avoiding hardcoded strings entirely.</span> </code></pre>
jsx
<Component title="Hello!" />
<pre class="language-text"><code class="language-text">code-block.jsx:1:18 <a href="https://biomejs.dev/linter/rules/no-jsx-literals">lint/style/noJsxLiterals</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Incorrect use of string literal detected.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>&lt;Component title=&quot;Hello!&quot; /&gt; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">String literals in JSX can make code harder to maintain and internationalize.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Consider avoiding hardcoded strings entirely.</span> </code></pre>

allowedStrings

An array of strings that are allowed as literals. This can be useful for common words or characters that don't need to be wrapped in expressions.

json
{
	"linter": {
		"rules": {
			"style": {
				"noJsxLiterals": {
					"options": {
						"allowedStrings": [
							"Hello",
							"&nbsp;",
							"·"
						]
					}
				}
			}
		}
	}
}

jsx
<>
  <div>Hello</div>
  <div>&nbsp;</div>
  <div>·</div>
</>

ignoreProps

When enabled, the rule will ignore string literals used as prop values.

Default: false

json
{
	"linter": {
		"rules": {
			"style": {
				"noJsxLiterals": {
					"options": {
						"ignoreProps": true
					}
				}
			}
		}
	}
}

jsx
<>
  <Component title="Welcome" />
  <input placeholder="Enter name" />
</>
</TabItem> </Tabs>