Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[React 19] React Compiler doesn't preserve HTML Entities #29100

Closed
Thanaen opened this issue May 16, 2024 · 5 comments
Closed

[React 19] React Compiler doesn't preserve HTML Entities #29100

Thanaen opened this issue May 16, 2024 · 5 comments

Comments

@Thanaen
Copy link

Thanaen commented May 16, 2024

Summary

When using React Compiler, this component:

export default function MyApp() {
  return <div>Parent &gt; Children</div>;
}

becomes

function MyApp() {
  const $ = _c(1);

  let t0;

  if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
    t0 = <div>Parent > Children</div>;
    $[0] = t0;
  } else {
    t0 = $[0];
  }

  return t0;
}

which leads to this error: ERROR: The character ">" is not valid inside a JSX element

Repro: https://playground.react.dev/#N4Igzg9grgTgxgUxALhAgHgBwjALgAgBMEAzAQygBsCSoA7OXASwjvwFkBPAQU0wAoAlPmAAdNvhgJcsNgB5CTAG4A+AApkpdAgDIA5rgDc+AMIALJpUJa5AekWrD4gL7iQzoA

@HoseinKhanBeigi
Copy link

To address the issue with React Compiler not preserving HTML entities, you can use a different approach to ensure that the > character is correctly rendered in your JSX. Instead of using the HTML entity >, you can use JavaScript's String.fromCharCode method to represent the character in a way that avoids the issue.
export default function MyApp() { return <div>Parent {String.fromCharCode(62)} Children</div>; }

@vemoo
Copy link

vemoo commented May 16, 2024

I think String.fromCharCode is not needed, this also seems to work:

export default function MyApp() {
  return <div>Parent {">"} Children</div>;
}

https://playground.react.dev/#N4Igzg9grgTgxgUxALhAgHgBwjALgAgBMEAzAQygBsCSoA7OXASwjvwFkBPAQU0wAoAlPmAAdNvhgJcsNgB5CTAG4A+AApkpdAmJArRIAL74AwgAsmlQlrkB6RaoDc4w+KNA

@Thanaen
Copy link
Author

Thanaen commented May 16, 2024

I think String.fromCharCode is not needed, this also seems to work:

export default function MyApp() {
  return <div>Parent {">"} Children</div>;
}

https://playground.react.dev/#N4Igzg9grgTgxgUxALhAgHgBwjALgAgBMEAzAQygBsCSoA7OXASwjvwFkBPAQU0wAoAlPmAAdNvhgJcsNgB5CTAG4A+AApkpdAmJArRIAL74AwgAsmlQlrkB6RaoDc4w+KNA

That seems like a nice workaround, thank you!

@josephsavona
Copy link
Contributor

Thanks for the heads up, we’ll fix!

josephsavona added a commit that referenced this issue May 17, 2024
Babel doesn't seem to properly preserve escaping of HTML entities when emitting JSX text children, so this commit works around the issue by emitting a JsxExpressionContainer for JSX children that contain ">", "<", or "&" characters.

Closes #29100

[ghstack-poisoned]
josephsavona added a commit that referenced this issue May 17, 2024
Babel doesn't seem to properly preserve escaping of HTML entities when emitting JSX text children, so this commit works around the issue by emitting a JsxExpressionContainer for JSX children that contain ">", "<", or "&" characters.

Closes #29100

ghstack-source-id: 2d0622397cc067c6336f3635073e07daef854084
Pull Request resolved: #29143
@josephsavona
Copy link
Contributor

Fixed, thanks for reporting!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants