161

This is my code:

const func = () => { return ( <div > you're free </div> )} 

Somehow ESLint flags the line "you're free" with error error HTML entities must be escaped react/no-unescaped-entities

However, from what I can see, JSX has escaped the apostrophes already. I can see the words you're free is rendered without issue. If I escape it as &#39;, then it will be very hard for me to search for the string (I would expect a search of you're free in an editor to return a hit. But obviously the editor will miss because the text is actually you&#39;re free)

So what is the best way to address this ESLint exception?

2
  • 5
    I prefer to be able to write you're free instead of something ridiculous like you&apos;re free. Is there any good reason why I cant just disable the react/no-unescaped-entities rule? Commented Feb 25, 2022 at 9:34
  • 1
    related GitHub issue Commented Feb 14, 2024 at 18:35

16 Answers 16

191

Recommended solution is to use &apos;, &lsquo; or &rsquo; instead of wrapping it as a variable. So like this:

const func = () => { return ( <div > you&apos;re free </div> )} 

For search-ability, it's recommended you have files for localization/internationalization and call them into your app.

Sign up to request clarification or add additional context in comments.

4 Comments

Another easy way is create the string as a separate variable. const func = () => { const text = `you're free`; return ( <div > {text} </div> )}; then eslint shouldn't complain about curly braces
I am searching for a solution which will auto-fix these issues with the --fix flag. I've a lot of these warnings and I can't do a find-and-replace as there are a lot of string literals in the same files which have a ' in them. Anyone got any suggestions?
I don't think it's super clear that this is actually better... This makes it a lot harder to read your markup. IMO do {you're free} so you're not a human reading machine code
Hi, about the "recommended solution" => by whom? What's the consequence of not escaping those characters? Edit: this link seems relevant: github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/… also show how to restrict the characters (I dislike escaping all apostrophes...)
117

By the way, you can disable this kind of warnings by adding

"rules": { "react/no-unescaped-entities": 0 } 

to your .eslintrc file.

2 Comments

Is there any reason I wouldn't want to do this? IE why is it a rule in the first place by default
@Andrew github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/… explains why it's a rule - it's supposed to help you from making silly mistakes
38

I had the same issue with next.js. I opened the .eslintrc.json and added the following:

{ "rules": { "react/no-unescaped-entities": 0 } } 

Now my .eslintrc.json will look as follows:

{ "extends": "next/core-web-vitals", "rules": { "react/no-unescaped-entities": 0 } } 

Comments

32

I explicitly escape the whole block of text by enclosing the line in {" "}:

const func = () => { return ( <div > {" you're free "} </div> )} 

2 Comments

Recommneded solution is to use &apos; instead of wrapping it as a variable.
[eslint] Curly braces are unnecessary here. (react/jsx-curly-brace-presence)
10

You can do two things,

  1. It can be escaped with &apos;

    E.g.,

     const func = () => { return ( <div > you&apos;re free </div> )} 
  2. Or you need to disable some ESLint rules. For disabling the ESLint rules, first find .eslintrc.json in the project directory, and then paste these lines there.

    { "extends": "next/core-web-vitals", "rules": { "react/no-unescaped-entities": "off", "@next/next/no-page-custom-font": "off" } } 

Comments

8

add this on the file that is causing the error

/* eslint-disable react/no-unescaped-entities */ 

also add this in the eslintrc.json file

{ "extends": "next/core-web-vitals", "rules": { "react/no-unescaped-entities": "off", "@next/next/no-page-custom-font": "off" } } 

Comments

7

The previous solutions work only in some cases. It wasn't working for me. In my case, I think it's because we're also using Prettier in our project. To resolve the error, I wrapped the copy in backticks.

const func = () => { return ( <div> {` You're free. `} </div> ) } 

1 Comment

[eslint] Curly braces are unnecessary here. (react/jsx-curly-brace-presence)
4

You can also use this extension in Visual Studio Code to fix these issues: html-entities

It is a good programming practice to not disable the HTML entity warning as it can help prevent some issues.

Comments

2

One way to fix the react/no-unescaped-entities error is by assigning the text with the escape character in it to a variable and then referencing the variable in the JXS.

Further reading:

const func = () => { const text = `you're free`; return ( <div> {text} </div> )} 

Comments

2

Using backtick is more elegant to avoid this mistake:

const func = () => { return ( <div > {`you're free`} </div> )} 

Comments

1

This works for me without any ESLint errors:

const func = () => { return ( <div> {"you're"} free </div> ) } 

2 Comments

Curly braces are unnecessary here (.react/jsx-curly-brace-presence}
That just gives another eslint error since the braces are unnecessary
1

You can add this line on top to disable the error for entire file

/* eslint-disable react/no-unescaped-entities */ 

Comments

1

You can disable this kind of warnings by adding the following setting to your eslint.config.js.

module.exports = { // Other configuration options... rules: { 'react/no-unescaped-entities': 'off', }, }; 

When using Vite to create a React project, an eslint.config.js file is generated to manage ESLint settings.


To verify

If your rule ('react/no-unescaped-entities': 'off') is correctly configured, ESLint will not display any warnings for unescaped entities in JSX.

ESLint

Comments

0

I encountered this issue in my upcoming project and successfully resolved it by encapsulating my code within backticks (``).

Comments

0

Problem:

I'm encountering the "react/no-unescaped-entities" error during Vercel deployment of my Next.js project. This error specifically targets apostrophes (') within JSX.

I've tried:

  1. Disabling the ESLint rule
  2. Ensuring HTML entities (like &apos;) are used.
  3. Checking for configuration conflicts.

These solutions work in some cases, but the error persisted for me.

Cause:

The root cause turned out to be an outdated eslint-config-next dependency. Upgrading this package resolved the deployment issue.

Solution

Identify Version: Check your current eslint-config-next version in your package.json file.

Upgrade: Update to the latest version using either npm or yarn:

npm install --save-dev eslint-config-next@latest

Hope it will work for you!

Comments

0

I see most people here like to put their whole text as a variable or the whole line in backticks. For me i just do put the quote in quotes ex: {"'"}. It is ugly but a lot less work for the same results.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.