Skip to content

Commit ba0d05a

Browse files
committed
add use-javascript-comment-in-jsx.md
1 parent e762ea1 commit ba0d05a

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Using JavaScript Expressions in JSX
2+
3+
You can place JavaScript comments anywhere in React/JSX you want except locations where JSX might expect a React child node. In this situation you'll have to escape the comment using { } so that JSX knows to pass that on as actual JavaScript.
4+
5+
Make sure you understand where you'll have to tell JSX to pass along a JS comment so a child React node is not created.
6+
7+
```javascript
8+
var reactNode = <div /*comment*/>{/* use {'{}'} here to comment*/}</div>;
9+
```
10+
11+
In the above code if I had not wrapped the comment inside of the `<div>` with `{ }` brackets then Babel would have converted the comment to a React text node. The outcome, unintentionally, without the `{ }` would be:
12+
13+
```javascript
14+
var reactNode = React.createElement(
15+
"div",
16+
null,
17+
"/* use ",
18+
"{}",
19+
" here to comment*/"
20+
);
21+
```
22+
23+
Which would result in the following HTML that would have unintended text nodes.
24+
25+
```javascript
26+
<div data-reactid=".0">
27+
<span data-reactid=".0.0">/* use </span>
28+
<span data-reactid=".0.1">{}</span>
29+
<span data-reactid=".0.2"> here to comment*/</span>
30+
</div>
31+
```

0 commit comments

Comments
 (0)