react/no-unescaped-entities
Disallow " and ' as plain characters in JSX text
A stray " or ' in JSX text is often a slip. A quote that was meant to end an attribute lands after the tag's >, and it shows up on the page.
If you do want the character, write it as an HTML entity, or put the text in a string inside braces. In an editor, the rule suggests the entities to replace it with.
Configure#
Off by default. To turn it on:
[lint.rules]
"react/no-unescaped-entities" = "error" # or "warn"Every option, set to its default. The ones that are commented out are unset by default.
[lint.rules]
"react/no-unescaped-entities" = ["error", {
# forbid = <list of strings or objects>,
}]Examples#
const label = <label htmlFor="name">"" can be escaped with ", “, ", ”. Your name</label>;const note = <p>Don'' can be escaped with ', ‘, ', ’.t close this window.</p>;const note = <p>Don't close this window.</p>;
const hint = <p>{"Don't close this window."}</p>;Options#
| Option | Default | What it does |
|---|---|---|
forbid | >, ", ', } | The characters to report. Your list replaces the default list. |
> and } in JSX text are syntax errors, so you get a parsing error for them, not a report from this rule.
Each item in forbid is a single character, or an object with the character and the entities to suggest: { "char": "\"", "alternatives": ["""] }.
Here only " is forbidden. The two quotes are reported and the apostrophe isn't:
{ "forbid": ["\""] }2 problemsconst note = <p>Don't press "HTML entity, " , must be escaped.Delete"HTML entity, " , must be escaped..</p>;When not to use it#
In text with many apostrophes and quotes, the entities make the source hard to read. Leave the rule off there, or shorten forbid.