111

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Anthony Kong
  • 37,791
  • 46
  • 172
  • 304
  • 2
    I prefer to be able to write you're free instead of something ridiculous like you're free. Is there any good reason why I cant just disable the react/no-unescaped-entities rule? – Andy Feb 25 '22 at 09:34

12 Answers12

142

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.

Noitidart
  • 35,443
  • 37
  • 154
  • 323
  • 3
    This is the best and cleanest way to fix this. Thanks! – Ru Chern Chong Dec 05 '18 at 08:58
  • Thank you @RuChernChong! :) – Noitidart Dec 05 '18 at 13:09
  • Another easy way is create the string as a separate variable. `const func = () => { const text = \`you're free\`; return (
    {text}
    )};` then eslint shouldn't complain about curly braces
    – aarowman Jun 03 '21 at 16:55
  • 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? – Abhijit Dec 16 '21 at 08:41
  • 2
    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 – ICW Nov 30 '22 at 21:23
86

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

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

to your .eslintrc file.

pensum
  • 980
  • 1
  • 11
  • 25
kiba
  • 1,355
  • 9
  • 7
  • 10
    Is there any reason I wouldn't want to do this? IE why is it a rule in the first place by default – Andy Feb 25 '22 at 09:34
  • 6
    @Andrew https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-unescaped-entities.md explains why it's a rule - it's supposed to help you from making silly mistakes – Kevin Connors Mar 08 '22 at 17:33
  • 1
    solved my problem tnx! – bami Jun 22 '22 at 05:48
27

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

const func = () => {
  return (
    <div >
       {" you're free "}
      </div>
  )}
Anthony Kong
  • 37,791
  • 46
  • 172
  • 304
23

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 }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
crispengari
  • 7,901
  • 7
  • 45
  • 53
8

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"
      }
    }
    
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Amit Singh
  • 609
  • 7
  • 7
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>
  )
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Farid
  • 1,557
  • 2
  • 21
  • 35
1

This works for me without any ESLint errors:

const func = () => {
  return (
    <div>
      {"you're"} free
    </div>
  )
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vic
  • 8,261
  • 6
  • 42
  • 55
1

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>
  )}
Randy Burden
  • 2,611
  • 1
  • 26
  • 34
1

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Peter
  • 1,124
  • 14
  • 17
0

Using backtick is more elegant to avoid this mistake:

const func = () => {
  return (
    <div >
       {`you're free`}
      </div>
  )}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

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

/* eslint-disable react/no-unescaped-entities */
j3rry
  • 145
  • 1
  • 1
  • 6
0

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"
  }
}