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

Proposal: jsx-no-mixed-title #3839

Open
jamesarosen opened this issue Oct 10, 2024 · 2 comments
Open

Proposal: jsx-no-mixed-title #3839

jamesarosen opened this issue Oct 10, 2024 · 2 comments

Comments

@jamesarosen
Copy link

This issue describes a common problem that has an easy but non-obvious solution.

  1. Browsers interpret the contents of <title> as literal text.
  2. React uses comments to mark the boundaries between static and dynamic content

Putting (1) and (2) together means that this normal-seeming JSX is broken:

<head>
  <title>Some static text {aVariable} more static text</title>
</head>

react-dom emits a warning:

Warning: A title element received an array with more than 1 element as children. In browsers title Elements can only have Text Nodes as children. If the children being rendered output more than a single text node in aggregate the browser will display markup and comments as text in the title and hydration will likely fail and fall back to client rendering

I propose a rule that allows static text or an interpolation, but not both as the child of <title>.

Valid:
<title>Some static text</title>

Valid:
<title>{aVariable}</title>

Valid:
<title>{`A Template string {isAGreatWay} to combine them`}</title>

Invalid:
<title>Some static text {aVariable} more static text</title>
@ljharb
Copy link
Member

ljharb commented Oct 11, 2024

tbh this seems like just a bug in React to me - i'm not sure why they can't just omit the comments for <title>, presumably at the insignificant cost of making updates to that element slower or more frequent.

@jamesarosen
Copy link
Author

I agree, but the React team knows about it and doesn't want to fix it.

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

No branches or pull requests

2 participants