Skip to content

noJsxLiterals

¥Summary

¥How to configure

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

¥Description

禁止在 JSX 元素中使用字符串字面量。

¥Disallow string literals inside JSX elements.

此规则不鼓励在 JSX 元素中直接使用字符串字面量。JSX 中的字符串字面量会使代码更难维护,尤其是在需要国际化或动态内容的应用中。

¥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

<div>Hello World</div>
code-block.jsx:1:6 lint/nursery/noJsxLiterals ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Incorrect use of string literal detected.

> 1 │ <div>Hello World</div>
^^^^^^^^^^^
2 │

String literals in JSX can make code harder to maintain and internationalize.

Consider avoiding hardcoded strings entirely.

<>Welcome to our site</>
code-block.jsx:1:3 lint/nursery/noJsxLiterals ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Incorrect use of string literal detected.

> 1 │ <>Welcome to our site</>
^^^^^^^^^^^^^^^^^^^
2 │

String literals in JSX can make code harder to maintain and internationalize.

Consider avoiding hardcoded strings entirely.

<span>
Please enter your name
</span>
code-block.jsx:1:7 lint/nursery/noJsxLiterals ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Incorrect use of string literal detected.

> 1 │ <span>

> 2 │ Please enter your name
> 3 │ </span>

4 │

String literals in JSX can make code harder to maintain and internationalize.

Consider avoiding hardcoded strings entirely.

¥Valid

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

¥Options

启用后,该规则还会标记 JSX 表达式和属性中的字符串字面量。

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

默认:false

¥Default: false

biome.json
{
"linter": {
"rules": {
"nursery": {
"noJsxLiterals": {
"options": {
"noStrings": true
}
}
}
}
}
}
<span>
{'Please enter your name'}
</span>
code-block.jsx:2:4 lint/nursery/noJsxLiterals ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Incorrect use of string literal detected.

1 │ <span>
> 2 │ {‘Please enter your name’}
^^^^^^^^^^^^^^^^^^^^^^^^
3 │ </span>
4 │

String literals in JSX can make code harder to maintain and internationalize.

Consider avoiding hardcoded strings entirely.

<Component title="Hello!" />
code-block.jsx:1:18 lint/nursery/noJsxLiterals ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Incorrect use of string literal detected.

> 1 │ <Component title=“Hello!” />
^^^^^^^^
2 │

String literals in JSX can make code harder to maintain and internationalize.

Consider avoiding hardcoded strings entirely.

允许作为字面量的字符串数组。对于不需要用表达式封装的常用单词或字符,这可能很有用。

¥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.

biome.json
{
"linter": {
"rules": {
"nursery": {
"noJsxLiterals": {
"options": {
"allowedStrings": [
"Hello",
"&nbsp;",
"·"
]
}
}
}
}
}
}
<>
<div>Hello</div>
<div>&nbsp;</div>
<div>·</div>
</>

启用后,该规则将忽略用作 prop 值的字符串字面量。

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

默认:false

¥Default: false

biome.json
{
"linter": {
"rules": {
"nursery": {
"noJsxLiterals": {
"options": {
"ignoreProps": true
}
}
}
}
}
}
<>
<Component title="Welcome" />
<input placeholder="Enter name" />
</>

¥Related links