--- id: docs-jsx-is-not-html title: JSX is not HTML description: Differences between JSX and HTML. layout: docs prev: api.html --- JSX looks like HTML but there are some important differences you may run into. ## Whitespace removal 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 `{' '}`. ```javascript {this.props.name} {' '} {this.props.surname}
``` This behavior is still being debated. Follow [Issue #65](https://github.com/facebook/react/issues/65) to be updated on the situation. ## HTML Entities You can insert HTML entities within literal text in JSX: ```javascript First · Second
``` 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. ```javascript // Bad: It displays \"First · Second\" {'First · Second'}
``` 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. ```javascript {'First \u00b7 Second'}
``` A safer alternative is to find the unicode number corresponding to the entity and use it inside of a Javascript string. ```javascript {'First u00b7 Second'}
{'First ' + String.fromCharCode(183) + ' Second'}
``` You can use mixed arrays with strings and JSX elements. ```javascript {['First ', ·, ' Second']}
``` In last resort, you always have the ability to insert raw HTML inside of the div. ```javascript ``` ## Comments JSX supports both single-line and multi-lines Javascript comments within a tag declaration: ```javascript ``` 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: ```javascript // Note: The following is not implemented yet! {/* This is a comment */}
``` ## Custom HTML attributes 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-`. ```javascript ``` [Web Accessibility](http://www.w3.org/WAI/intro/aria) attributes starting with `aria-` will be rendered properly. ```javascript ``` "], "neg": []}
{"query": "Someone had the trouble in IRC about it. Edit: changed the subject from \"document it\" to \"support it\", because the documentation issue is already at .\nBetter yet, support it!\nYeah, is there a particular reason it's not supported?\nCurious if you've seen the . It's automatically injected into React by default, so you can use it today. IMHO it's (much) better than . (I suppose we could support just for the sake of completeness, but I thought I'd just make sure you guys were aware of .)\nWe discussed this for a bit on IRC -- was just confused that silently didn't work whereas other events did -- perhaps adding more documentation () will fix the problem too but it seems like we might as well add for completeness. (I agree that enter/leave are much more useful and suggested that over/out are likely missing simply because no one has needed them!)\nAgree - I believe there are a few extra allocations (not-pooled) occuring in the event system already today. I'd love to track those down before adding more mouse-move event allocations. If you open the memory profiler in Chrome and move the mouse around, you'll see memory being allocated (and then properly freed of course) but if we use es everywhere, we should be flat. That would make me much more comfortable with adding to . Edit: Just checked out - we already have which fires way more frequently than would so it's probably not such a big deal to add - though we really should get rid of all additional allocations in the event path.\nJust started to look a little bit into the allocations here. Redefining trapBubbledEvent in ReactEventEmitter with: still shows allocations in the Chrome memory timeline view, so I'm not convinced that there's anything we can do here? (Replacing trapBubbledEvent itself with a noop makes the allocations go away. simply calls addEventListener.)\nI am pretty sure we have done all we can here -- at least I think we've tracked down every callsite where PooledClass would have helped us. I updated our recast transforms to log every syntax construct that would cause an obvious allocation and couldn't find anything. I was thinking maybe there was an array slice or a bind() in there, but experiment shows that this is not the case.\n(I didn't verify that we don't have more allocations than the empty-function case; I don't know of a good way to do so.)\nI might be misinformed, but isn't there quite a difference in use cases between mouseenter and mouseover events? Here's a theoretical (non-react) example working with some hover states for nested elements: I tried to apply the above concept in react (with nested components) using onMouseEnter instead, but it obviously fails because it's only triggered once on the top-most element. Moving into a child element and back has no effect. Maybe there's a better approach for this in react altogether?\nIf you render the child hierarchy with React, you can listen to on each child element you are interested in instead of using event bubbling. It is cheap.", "pos": ["``` onClick onDoubleClick onDrag onDragEnd onDragEnter onDragExit onDragLeave onDragOver onDragStart onDrop onMouseDown onMouseEnter onMouseLeave