2023-05-03 11:06:20 +02:00
== Why is this an issue?
2023-07-04 12:16:24 +02:00
JSX lets you write HTML-like markup inside a JavaScript file, commonly used in React.
2022-07-07 12:02:31 +02:00
Adding comments inside JSX might be tricky as JSX code is neither a plain HTML nor JavaScript.
2023-07-04 12:16:24 +02:00
HTML comments (``++<!-- comment here -->++``) are not valid syntax in JSX.
2022-07-07 12:02:31 +02:00
2023-07-04 12:16:24 +02:00
JavaScript-style comments, single or multiline, will create an additional text node in the browser, which is probably not expected.
2022-07-07 12:02:31 +02:00
[source,javascript]
----
<div>
2023-07-04 12:16:24 +02:00
// Noncompliant: text inside node
2022-07-07 12:02:31 +02:00
</div>
----
2023-07-04 12:16:24 +02:00
To avoid that, use JavaScript multiline comments enclosed in curly braces. Single-line comments can also be used, but avoid having the ending bracket in the same line.
2022-07-07 12:02:31 +02:00
[source,javascript]
----
<div>
2023-07-04 12:16:24 +02:00
{
/*
multi-line
comment
*/
}
{
// single-line comment
}
{ /* short form comment */ }
2022-07-07 12:02:31 +02:00
</div>
----
2023-07-04 12:16:24 +02:00
Note that JavaScript comments around attributes are also allowed (`<div /* comment */>`).
If the additional text node is intentional, prefer using a JavaScript string literal containing that comment.
2022-07-07 12:02:31 +02:00
[source,javascript]
----
<div>
2023-07-04 12:16:24 +02:00
{ '// text inside node' }
2022-07-07 12:02:31 +02:00
</div>
----
2023-07-04 12:16:24 +02:00
== Resources
=== Documentation
2023-10-19 11:46:59 +02:00
* React Documentation - https://react.dev/learn/javascript-in-jsx-with-curly-braces[JavaScript in JSX with Curly Braces]