Skip to content

Commit 388c8a5

Browse files
vjeuxzpao
authored andcommitted
Adding JSX pitfalls section in the docs
1 parent dadb3f7 commit 388c8a5

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

docs/_includes/nav_docs.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,11 @@ <h3>Reference</h3>
2121
<li><a href="/react/docs/api.html"{% if page.id == 'docs-api' %} class="active"{% endif %}>API</a></li>
2222
</ul>
2323
</div>
24+
25+
<div class="nav-docs-section">
26+
<h3>Appendix</h3>
27+
<ul>
28+
<li><a href="/react/docs/jsx-is-not-html.html"{% if page.id == 'docs-jsx-is-not-html' %} class="active"{% endif %}>JSX is not HTML</a></li>
29+
</ul>
30+
</div>
2431
</div>

docs/docs/api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ id: docs-api
33
title: React API
44
layout: docs
55
prev: mixins.html
6+
next: jsx-is-not-html.html
67
---
78

89
## React

docs/docs/jsx-is-not-html.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
---
2+
id: docs-jsx-is-not-html
3+
title: JSX is not HTML
4+
description: Differences between JSX and HTML.
5+
layout: docs
6+
prev: api.html
7+
---
8+
9+
JSX looks like HTML but there are some important differences you may run into.
10+
11+
## Whitespace removal
12+
13+
JSX doesn't follow the same whitespace elimination rules as HTML. JSX removes all the whitespaces between two curly braces expressions. If you want to have a white space, a work-around is to add `{' '}`.
14+
15+
```javascript
16+
<div>{this.props.name} {' '} {this.props.surname}</div>
17+
```
18+
19+
This behavior is still being debated. Follow [Issue #65](https://github.com/facebook/react/issues/65) to be updated on the situation.
20+
21+
## HTML Entities
22+
23+
You can insert HTML entities within literal text in JSX:
24+
25+
```javascript
26+
<div>First &middot; Second</div>
27+
```
28+
29+
If you want to display an HTML entity within a dynamic content, you will run into double escaping issues as React escapes all the strings you are displaying in order to prevent a wide range of XSS attacks by default.
30+
31+
```javascript
32+
// Bad: It displays "First &middot; Second"
33+
<div>{'First &middot; Second'}</div>
34+
```
35+
36+
There are various ways to work-around this issue. The easiest one is to write unicode character directly in Javascript. You've got to make sure that the file is saved as UTF-8 and that the propers UTF-8 directives are set so the browser will display it correctly.
37+
38+
```javascript
39+
<div>{'First · Second'}</div>
40+
```
41+
42+
A safer alternative is to find the <a href="http://www.fileformat.info/info/unicode/char/b7/index.htm">unicode number corresponding to the entity</a> and use it inside of a Javascript string.
43+
44+
```javascript
45+
<div>{'First \u00b7 Second'}</div>
46+
<div>{'First ' + String.fromCharCode(183) + ' Second'}</div>
47+
```
48+
49+
You can use mixed arrays with strings and JSX elements.
50+
51+
```javascript
52+
<div>{['First ', <span>&middot;</span>, ' Second']}</div>
53+
```
54+
55+
In last resort, you always have the ability to insert raw HTML inside of the div.
56+
57+
```javascript
58+
<div dangerouslySetInnerHTML={{'{{'}}__html: 'First &middot; Second'}} />
59+
```
60+
61+
## Comments
62+
63+
JSX supports both single-line and multi-lines Javascript comments within a tag declaration:
64+
65+
```javascript
66+
<div // This is a single line comment:
67+
/*
68+
And a multiline
69+
comment
70+
*/
71+
/>
72+
```
73+
74+
As of React 0.3, there is no good way to insert comments within the children section. [Issue #82](https://github.com/facebook/react/issues/82) is tracking progress to enable the following way to write comments:
75+
76+
```javascript
77+
// Note: The following is not implemented yet!
78+
<div>
79+
{/* This is a comment */}
80+
</div>
81+
```
82+
83+
## Custom HTML attributes
84+
85+
If you pass properties to native HTML elements that do not exist in the HTML specification, React will not render them. If you want to use a custom attribute, you should prefix it with `data-`.
86+
87+
```javascript
88+
<div data-custom-attribute="foo" />
89+
```
90+
91+
[Web Accessibility](http://www.w3.org/WAI/intro/aria) attributes starting with `aria-` will be rendered properly.
92+
93+
```javascript
94+
<div aria-hidden={true} />
95+
```

0 commit comments

Comments
 (0)