|
| 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 · 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 · Second" |
| 33 | +<div>{'First · 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>·</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 · 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